MyBatis
新建工程,选好资源
实体类
不需要添加各种注解,直接把对应字段生成get与set方法即可
package com.bruce.SpringBootMVC04Mybatis.entity;
import java.io.Serializable;
public class Account implements Serializable {
private static final long serialVersionUID = -8149090919935604147L;
private Integer id;
private String loginName;
private String password;
private String nickName;
private Integer age;
private String location;
private String role;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getLoginName() {
return loginName;
}
public void setLoginName(String loginName) {
this.loginName = loginName;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getNickName() {
return nickName;
}
public void setNickName(String nickName) {
this.nickName = nickName;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public String getLocation() {
return location;
}
public void setLocation(String location) {
this.location = location;
}
public String getRole() {
return role;
}
public void setRole(String role) {
this.role = role;
}
public Account() {
super();
}
}
Dao层接口定义
在接口上要加上@Mapper注解
package com.bruce.SpringBootMVC04Mybatis.mapper;
import java.util.List;
import org.apache.ibatis.annotations.Mapper;
import com.bruce.SpringBootMVC04Mybatis.entity.Account;
@Mapper
public interface AccountMapper {
List<Account> findAll();
}
注:如果这里不想每个接口都加注解的话,可以在SpringBoot启动类上面加上注解@MapperScan("com.bruce.SpringBootMVC04Mybatis.mapper"),括号中对应Dao层的路径,这样每个Dao接口上面就不用加@Mapper注解了
Service层实现定义
在类名上加上@Service注解,在注入的Dao接口对象上加上@Autowired注解
package com.bruce.SpringBootMVC04Mybatis.service;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.bruce.SpringBootMVC04Mybatis.entity.Account;
import com.bruce.SpringBootMVC04Mybatis.mapper.AccountMapper;
@Service
public class AccountService {
@Autowired
AccountMapper accountMapper;
public List<Account> findAll() {
return accountMapper.findAll();
}
}
Controller层定义
在Controller类上加上@RestController注解,在注入的Service对象加上注解@Autowired
对应的Controller方法上加上@RequestMapping注解来配置请求路径
package com.bruce.SpringBootMVC04Mybatis.controller;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import com.bruce.SpringBootMVC04Mybatis.entity.Account;
import com.bruce.SpringBootMVC04Mybatis.service.AccountService;
@RestController
@RequestMapping("/account")
public class AccountController {
@Autowired
AccountService accountService;
@RequestMapping("/list")
@ResponseBody
public Object list() {
List<Account> accounts = accountService.findAll();
return accounts;
}
}
配置MyBatis的xml配置文件
配置文件中mapper节点的namespace属性对应Dao层接口路径,resultMap节点的type属性对应实体类对象路径
<?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.bruce.SpringBootMVC04Mybatis.mapper.AccountMapper">
<resultMap id="BaseResultMap"
type="com.bruce.SpringBootMVC04Mybatis.entity.Account">
<id column="id" jdbcType="INTEGER" property="id" />
<result column="login_name" jdbcType="VARCHAR"
property="loginName" />
<result column="password" jdbcType="VARCHAR"
property="password" />
<result column="nick_name" jdbcType="VARCHAR"
property="nickName" />
<result column="age" jdbcType="INTEGER" property="age" />
<result column="location" jdbcType="VARCHAR"
property="location" />
<result column="role" jdbcType="VARCHAR" property="role" />
</resultMap>
<select id="findAll" resultMap="BaseResultMap">
select * from account
</select>
</mapper>
在application.properties文件中进行相关配置
spring.datasource.url=jdbc:mysql://localhost:3306/ssm?characterEncoding=utf8&useSSL=false&serverTimezone=UTC
spring.datasource.username=root
spring.datasource.password=root
server.servlet.context-path = /SpringBoot
mybatis.type-aliases-package=com.bruce.SpringBootMVC04Mybatis.mapper (对应dao层接口的路径)
mybatis.mapper-locations=classpath:mybatis/mapper/*.xml (对应MyBatis的xml配置文件的路径)
一些基础代码,可以通过代码生成器来完成,这样基础的代码就不用自己再去手动写了
mybatis-generator-gui 使用说明与下载地址:https://github.com/zouzg/mybatis-generator-gui
生成的代码中Dao层的基类MyBatisBaseDao包含了所有的基本方法
/**
* DAO公共基类,由MybatisGenerator自动生成请勿修改
* @param <Model> The Model Class 这里是泛型不是Model类
* @param <PK> The Primary Key Class 如果是无主键,则可以用Model来跳过,如果是多主键则是Key类
* @param <E> The Example Class
*/
public interface MyBatisBaseDao<Model, PK extends Serializable, E> {
long countByExample(E example);
int deleteByExample(E example);
int deleteByPrimaryKey(PK id);
int insert(Model record);
int insertSelective(Model record);
List<Model> selectByExample(E example);
Model selectByPrimaryKey(PK id);
int updateByExampleSelective(@Param("record") Model record, @Param("example") E example);
int updateByExample(@Param("record") Model record, @Param("example") E example);
int updateByPrimaryKeySelective(Model record);
int updateByPrimaryKey(Model record);
}
每个实体类对应Dao层的mapper都有一个对应的实体类的Example,Service层可以调用mapper的基本增删改查方法,当需要查询列表数据的时候,可以调用selectByExample方法
@Service
public class MenuService {
@Autowired
MenuMapper menuMapper;
public List<Menu> findAll(String name) {
MenuExample menuExample = new MenuExample();
menuExample.createCriteria().andNameEqualTo(name);
return menuMapper.selectByExample(menuExample);
}
public Menu findById(Integer id) {
return menuMapper.selectByPrimaryKey(id);
}
}
通过pagehelper进行分页操作
pagehelper 使用说明文档 https://github.com/pagehelper/pagehelper-spring-boot
如果你使用 Maven,你只需要在 pom.xml 中添加下面的依赖:
<!--分页插件 https://mvnrepository.com/artifact/com.github.pagehelper/pagehelper-spring-boot-starter -->
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper-spring-boot-starter</artifactId>
<version>1.2.13</version>
</dependency>
需要加入分页的方法,可以如下进行修改
@Service
public class MenuService {
@Autowired
MenuMapper menuMapper;
public List<Menu> findAll(String name) {
MenuExample menuExample = new MenuExample();
menuExample.createCriteria().andNameEqualTo(name);
return menuMapper.selectByExample(menuExample);
}
public Menu findById(Integer id) {
return menuMapper.selectByPrimaryKey(id);
}
public List<Menu> findByPage(Integer pageNum, Integer pageSize) {
PageHelper.startPage(pageNum, pageSize);
MenuExample menuExample=new MenuExample();
return menuMapper.selectByExample(menuExample);
}
}
网友评论