1、配置pom.xml,增加mybatis依赖。
<dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>1.3.2</version> </dependency>
2、application.properties 添加数据库连接配置。
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/test?useUnicode=true&characterEncoding=UTF-8&serverTimezone=GMT%2B8 spring.datasource.username=root spring.datasource.password=root spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
3、启动类DemoApplication添加对mapper包扫描@MapperScan。
package com.idodo.demo; import org.mybatis.spring.annotation.MapperScan; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.builder.SpringApplicationBuilder; import org.springframework.boot.web.servlet.support.SpringBootServletInitializer; @SpringBootApplication @MapperScan("com.idodo.demo.mapper") public class DemoApplication extends SpringBootServletInitializer { @Override protected SpringApplicationBuilder configure(SpringApplicationBuilder application) { return application.sources(DemoApplication.class); } public static void main(String[] args) { SpringApplication.run(DemoApplication.class, args); } }
4、实体类User.java。
package com.idodo.demo.entity; import java.io.Serializable; public class User implements Serializable{ private static final long serialVersionUID = 7914100053493869907L; private Integer user_id ; private String user_code ; private String user_name ; public User() { } public Integer getUser_id() { return user_id; } public void setUser_id(Integer user_id) { this.user_id = user_id; } public String getUser_code() { return user_code; } public void setUser_code(String user_code) { this.user_code = user_code; } public String getUser_name() { return user_name; } public void setUser_name(String user_name) { this.user_name = user_name; } }
5、mapper文件配置UserMapper.java。
package com.idodo.demo.mapper; import java.util.List; import org.apache.ibatis.annotations.Delete; import org.apache.ibatis.annotations.Insert; import org.apache.ibatis.annotations.Result; import org.apache.ibatis.annotations.Results; import org.apache.ibatis.annotations.Select; import org.apache.ibatis.annotations.Update; import com.idodo.demo.entity.User; public interface UserMapper { @Select("select * from app_users where user_id = #{user_id}") @Results({ @Result(property = "user_code", column = "user_code"), @Result(property = "user_name", column = "user_name") }) User findUserById(Integer user_id); @Select("select * from app_users") @Results({ @Result(property = "user_code", column = "user_code"), @Result(property = "user_name", column = "user_name") }) List<User> getUserList(); @Insert("insert into app_users(user_code, user_name) values(#{user_code}, #{user_name})") void insert(User user); @Update("update app_users set user_code=#{user_code}, user_name=#{user_name} where user_id =#{user_id}") void update(User user); @Delete("delete FROM app_users where user_id =#{user_id}") void delete(Integer user_id); }
6、控制类UserController.java。
package com.idodo.demo.controller; import java.util.List; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; import com.idodo.demo.entity.User; import com.idodo.demo.mapper.UserMapper; import com.idodo.demo.service.UserService; @Controller public class UserController { @Autowired private UserMapper userMapper; @RequestMapping("/test/{user_id}") public @ResponseBody User testUser(@PathVariable Integer user_id) { User user = userMapper.findUserById(user_id); System.out.println(user.getUser_code() + user.getUser_name()); List<User> list = userMapper.getUserList(); for (User u : list) { System.out.println(u.getUser_name()); } User uu = new User(); uu.setUser_id(1); uu.setUser_code("9001"); uu.setUser_name("jzh"); userMapper.update(uu); User vo = new User(); vo.setUser_id(1); vo.setUser_code("9009"); vo.setUser_name("jiangzhihao20181217"); userMapper.insert(vo); return user; } }
7、启动DemoApplication,访问测试。
http://localhost:8080/test/1