用户从页面前端,也就是我们所说的 view 层进行查询访问,进入到 controller 层找到对应的接口,接 着 controller 进行对 service 层进行业务功能的调用,service 要进入 dao 层查询数据,dao 层调用 mapper.xml 文件生成 sql 语句到数据库中进行查询
与数据库的字段一一对应
@Getter @Setter public class User { private int id; private String username; private String password; private int age; }
@Repository注解修饰哪个类,则表明这个类具有对对象进行CRUD(增删改查)的功能
@Repository public interface UserDao { public User getUserById(@Param("id") int id); public List<User> getUserByAge(@Param("age") int age); public List<User> getUserByName(@Param("username") String username); public int insertUser(@RequestBody User user); }
通过UserMapping.xml配置文件实现UserDao接口
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="com.example.demo.dao.UserDao"> <select id="getUserById" resultType="User"> select * from `user` where id=#{id} </select> <select id="getUserByAge" resultType="User"> select * from `user` where age=#{age} </select> <select id="getUserByName" resultType="User"> select * from `user` where username like concat('%',#{username},'%') </select> <insert id="insertUser" parameterType="User"> <selectKey keyProperty="id" order="AFTER" resultType="int"> SELECT LAST_INSERT_ID() </selectKey> insert into user (username, password, age) values (#{username,jdbcType=VARCHAR}, #{password,jdbcType=VARCHAR}, #{age,jdbcType=INTEGER}) </insert> </mapper>
public interface UserService { public User queryUserById(int id); public List<User> queryUserByAge(int age); public List<User> queryUserByName(String username); public int insertUser(User user); }
再创建一个UserServiceImpl实现UserService
@Service public class UserSeviceImpl implements UserService { @Autowired private UserDao userDao; @Override public User queryUserById(int id) { return userDao.getUserById(id); } @Override public List<User> queryUserByAge(int age) { return userDao.getUserByAge(age); } @Override public List<User> queryUserByName(String username) { return userDao.getUserByName(username); } @Override public int insertUser(User user) { return userDao.insertUser(user); } }
@GetMapping是代表该查询接口用的是get方式
@RequestMapping(value="/insert",method = RequestMethod.POST)代表该插入接口用post方式
@RestController public class UserController { @Autowired private UserService userService; @GetMapping("/userid") public User getUserById(@RequestParam("id") int id){ return userService.queryUserById(id); } @GetMapping("/username") public List<User> getUserByUsername(@RequestParam("username") String username){ return userService.queryUserByName(username); } @RequestMapping(value="/insert",method = RequestMethod.POST) public User insertUser(@RequestBody User user){ int x = userService.insertUser(user); return user; } }
server: port: 8080 spring: datasource: driver-class-name: com.mysql.cj.jdbc.Driver url: jdbc:mysql://localhost:3306/testingdev9?useUnicode=true&characterEncoding=utf-8&serverTimezone=Asia/Shanghai username: root password: 123456 mybatis: mapper-locations: - classpath:mapper/*.xml - classpath*:com/**/mapper/*.xml type-aliases-package: com.example.demo.model
出现Started DemoApplication代表启动成功了
如下图根据id查询
如下图插入数据
可以看到数据库多了一条数据