diff --git a/mybatis-test/pom.xml b/mybatis-test/pom.xml new file mode 100644 index 0000000..242becb --- /dev/null +++ b/mybatis-test/pom.xml @@ -0,0 +1,71 @@ + + + 4.0.0 + + com.example + mybatis-test + 0.0.1-SNAPSHOT + jar + + mybatis-test + Demo project for Spring Boot + + + org.springframework.boot + spring-boot-starter-parent + 2.0.3.RELEASE + + + + + UTF-8 + UTF-8 + 1.8 + + + + + org.springframework.boot + spring-boot-starter-web + + + mysql + mysql-connector-java + runtime + + + org.mybatis.spring.boot + mybatis-spring-boot-starter + 1.3.2 + + + + com.alibaba + druid-spring-boot-starter + 1.1.9 + + + + com.github.pagehelper + pagehelper-spring-boot-starter + 1.2.5 + + + org.springframework.boot + spring-boot-starter-test + test + + + + + + + org.springframework.boot + spring-boot-maven-plugin + + + + + + diff --git a/mybatis-test/src/main/java/com/example/mybatistest/MybatisTestApplication.java b/mybatis-test/src/main/java/com/example/mybatistest/MybatisTestApplication.java new file mode 100644 index 0000000..c8c72ff --- /dev/null +++ b/mybatis-test/src/main/java/com/example/mybatistest/MybatisTestApplication.java @@ -0,0 +1,14 @@ +package com.example.mybatistest; + +import org.mybatis.spring.annotation.MapperScan; +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +@SpringBootApplication +@MapperScan("com.example.mybatistest.dao") +public class MybatisTestApplication { + + public static void main(String[] args) { + SpringApplication.run(MybatisTestApplication.class, args); + } +} diff --git a/mybatis-test/src/main/java/com/example/mybatistest/controller/UserController.java b/mybatis-test/src/main/java/com/example/mybatistest/controller/UserController.java new file mode 100644 index 0000000..6d3b194 --- /dev/null +++ b/mybatis-test/src/main/java/com/example/mybatistest/controller/UserController.java @@ -0,0 +1,45 @@ +package com.example.mybatistest.controller; + +import com.example.mybatistest.entity.User; +import com.example.mybatistest.service.UserService; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.web.bind.annotation.*; + +import java.util.List; + +/** + * Created with IntelliJ IDEA. + * Description: + * User: ${fxb} + * Email: fanxb.tl@gmail.com + * Date: 2018-07-30 + */ +@RestController +public class UserController { + + @Autowired + private UserService userService; + + @GetMapping("/user/{userId}") + public User getUser(@PathVariable String userId){ + return userService.getByUserId(userId); + } + + @GetMapping("/user") + public List getAll(){ + return userService.getAll(); + } + + @GetMapping("/user/page/{pageNum}") + public Object getPage(@PathVariable int pageNum, + @RequestParam(name = "pageSize",required = false,defaultValue = "10") int pageSize){ + return userService.getAll(pageNum,pageSize); + } + + @PostMapping("/user") + public Object addOne(User user){ + userService.insert(user); + return user; + } + +} diff --git a/mybatis-test/src/main/java/com/example/mybatistest/dao/UserDao.java b/mybatis-test/src/main/java/com/example/mybatistest/dao/UserDao.java new file mode 100644 index 0000000..d06ad5b --- /dev/null +++ b/mybatis-test/src/main/java/com/example/mybatistest/dao/UserDao.java @@ -0,0 +1,28 @@ +package com.example.mybatistest.dao; + +import com.example.mybatistest.entity.User; +import org.apache.ibatis.annotations.Insert; +import org.apache.ibatis.annotations.Options; +import org.apache.ibatis.annotations.Select; + +import java.util.List; + +/** + * Created with IntelliJ IDEA. + * Description: + * User: ${fxb} + * Email: fanxb.tl@gmail.com + * Date: 2018-07-30 + */ +public interface UserDao { + //插入用户 + @Insert("insert into user(name,age,password) value(#{name},#{age},#{password})") + @Options(useGeneratedKeys=true,keyColumn="id",keyProperty="id") + int insert(User user); + //根据id查询 + @Select("select * from user where id=#{id}") + User selectById(String id); + //查询所有 + @Select("select * from user") + List selectAll(); +} diff --git a/mybatis-test/src/main/java/com/example/mybatistest/entity/User.java b/mybatis-test/src/main/java/com/example/mybatistest/entity/User.java new file mode 100644 index 0000000..31516b8 --- /dev/null +++ b/mybatis-test/src/main/java/com/example/mybatistest/entity/User.java @@ -0,0 +1,55 @@ +package com.example.mybatistest.entity; + +/** + * Created with IntelliJ IDEA. + * Description: + * User: ${fxb} + * Email: fanxb.tl@gmail.com + * Date: 2018-07-30 + */ +public class User { + private int id; + private String name; + private int age; + private String password; + + public User(int id, String name, int age, String password) { + this.id = id; + this.name = name; + this.age = age; + this.password = password; + } + public User(){} + + public int getId() { + return id; + } + + public void setId(int id) { + this.id = id; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public int getAge() { + return age; + } + + public void setAge(int age) { + this.age = age; + } + + public String getPassword() { + return password; + } + + public void setPassword(String password) { + this.password = password; + } +} diff --git a/mybatis-test/src/main/java/com/example/mybatistest/service/UserService.java b/mybatis-test/src/main/java/com/example/mybatistest/service/UserService.java new file mode 100644 index 0000000..1eb03b7 --- /dev/null +++ b/mybatis-test/src/main/java/com/example/mybatistest/service/UserService.java @@ -0,0 +1,45 @@ +package com.example.mybatistest.service; + +import com.example.mybatistest.dao.UserDao; +import com.example.mybatistest.entity.User; +import com.github.pagehelper.PageHelper; +import com.github.pagehelper.PageInfo; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Component; + +import java.util.List; + +/** + * Created with IntelliJ IDEA. + * Description: + * User: ${fxb} + * Email: fanxb.tl@gmail.com + * Date: 2018-07-30 + */ +@Component +public class UserService { + + @Autowired + private UserDao userDao; + + public User getByUserId(String id){ + return userDao.selectById(id); + } + //获取全部用户 + public List getAll(){ + return userDao.selectAll(); + } + //测试分页 + public PageInfo getAll(int pageNum,int pageSize){ + PageHelper.startPage(pageNum,pageSize); + List users = userDao.selectAll(); + System.out.println(users.size()); + PageInfo result = new PageInfo<>(users); + return result; + } + + public int insert(User user){ + return userDao.insert(user); + } + +} diff --git a/mybatis-test/src/main/resources/application.yml b/mybatis-test/src/main/resources/application.yml new file mode 100644 index 0000000..878acdf --- /dev/null +++ b/mybatis-test/src/main/resources/application.yml @@ -0,0 +1,46 @@ +mybatis: + #对应实体类路径 + type-aliases-package: com.example.mybatistest.entity + #对应mapper映射文件路径 +# mapper-locations: classpath:mapper/*.xml + +#pagehelper配置 +pagehelper: + helper-dialect: mysql + reasonable: true + support-methods-arguments: true + params: count=countSql + returnPageInfo: check + +server: + port: 8081 + +spring: + datasource: + name: mysqlTest + type: com.alibaba.druid.pool.DruidDataSource + #druid相关配置 + druid: + #监控拦截统计的filters + filters: stat + driver-class-name: com.mysql.jdbc.Driver + url: jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8&useSSL=true + username: root + password: 123456 + #配置初始化大小,最小,最大 + initial-size: 1 + min-idle: 1 + max-active: 20 + #获取连接等待超时时间 + max-wait: 6000 + #间隔多久检测一次需要关闭的空闲连接 + time-between-eviction-runs-millis: 60000 + #一个连接在池中的最小生存时间 + min-evictable-idle-time-millis: 300000 + #打开PSCache,并指定每个连接上PSCache的大小。oracle设置为true,mysql设置为false。分库分表设置较多推荐设置 + pool-prepared-statements: false + max-pool-prepared-statement-per-connection-size: 20 + http: + encoding: + charset: utf-8 + enabled: true diff --git a/mybatis-test/src/main/resources/mapper/UserMapper.xml b/mybatis-test/src/main/resources/mapper/UserMapper.xml new file mode 100644 index 0000000..08bf682 --- /dev/null +++ b/mybatis-test/src/main/resources/mapper/UserMapper.xml @@ -0,0 +1,41 @@ + + + + + user + + + id,name,age,password + + + + INSERT INTO + + name,password, + + age + + + + #{name,jdbcType=VARCHAR},#{password}, + + #{age} + + + + + + + + \ No newline at end of file diff --git a/mybatis-test/src/test/java/com/example/mybatistest/MybatisTestApplicationTests.java b/mybatis-test/src/test/java/com/example/mybatistest/MybatisTestApplicationTests.java new file mode 100644 index 0000000..abe689d --- /dev/null +++ b/mybatis-test/src/test/java/com/example/mybatistest/MybatisTestApplicationTests.java @@ -0,0 +1,16 @@ +package com.example.mybatistest; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.test.context.junit4.SpringRunner; + +@RunWith(SpringRunner.class) +@SpringBootTest +public class MybatisTestApplicationTests { + + @Test + public void contextLoads() { + } + +}