步骤:
(1)在pom.xml文件中引入相关依赖;
(2)启动类App.java加@MapperScan注解
(3)在application.properties添加配置文件;
(4)编写User测试类;
(5)编写UserMapper;
(6)编写UserService
(7)编写UserController;
1、加入相关依赖:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
| <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.4.1.RELEASE</version> </parent>
<properties> <java.version>1.7</java.version> </properties>
<dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency>
<dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> </dependency>
<dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>1.1.1</version> </dependency>
</dependencies>
|
2、在 cn.river 包下新建启动类App.java。
加@MapperScan注解(mapper文件在cn.river.mapper包下)
1 2 3 4 5 6 7 8
| @SpringBootApplication @MapperScan("cn.river.mapper") public class App { public static void main(String[] args) { SpringApplication.run(App.class) ; } }
|
3、在application.properties文件中加入如下数据库配置
1 2 3 4 5 6 7 8 9 10 11
|
spring.datasource.url=jdbc:mysql://localhost:3306/test spring.datasource.username=root spring.datasource.password=root spring.datasource.driverClassName=com.mysql.jdbc.Driver spring.datasource.max-active=20 spring.datasource.max-idle=8 spring.datasource.min-idle=8 spring.datasource.initial-size=10
|
4、在 cn.river.model 包下新增Model: User.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
| public class User { private Integer id; private String name; private Integer age; public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } }
|
5、在 cn.river.mapper 包下新增UserMapper.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| public interface UserMapper { @Select("select * from user where id=#{id}") public User selectUserById(Integer id) ; @Select("select * from user where `NAME`=#{name}") public User selectUserByName(String name) ; @Select("select * from user where age=#{age}") public List<User> selectUserByAge(Integer age) ;
@Options(useGeneratedKeys = true, keyProperty = "id", keyColumn = "ID") @Insert("insert into user(`NAME`,AGE) values(#{name},#{age})") public void insertUser(User user) ; }
|
6、在cn.river.service包下新增UserService.java:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
| @Service public class UserService { @Autowired private UserMapper userMapper ; public User selectUserByName(String name) { return userMapper.selectUserByName(name) ; } public User selectUserById(Integer id) { return userMapper.selectUserById(id) ; } public List<User> selectUserByAge(Integer age) { return userMapper.selectUserByAge(age) ; } public void insertUser(User user) { userMapper.insertUser(user) ; } }
|
7、在cn.river.controller包下新增UserController.java:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
| @RestController public class UserController { @Autowired private UserService userService ; @RequestMapping("user/name") public User getUser(String name) { User user = userService.selectUserByName(name) ; return user ; } @RequestMapping("user/id") public User getUser(Integer id) { User user = userService.selectUserById(id) ; return user ; } @RequestMapping("user/age") public List<User> getUserByAge(Integer age) { PageHelper.startPage(1, 2) ; List<User> users = userService.selectUserByAge(age) ; return users ; } @RequestMapping("user/add") public User getUserByAge(String name,Integer age) { User user = new User() ; user.setName(name); user.setAge(age); userService.insertUser(user) ; return user ; } }
|