MyBatis
新建工程,選好資源
實體類
不需要添加各種注解犁跪,直接把對應(yīng)字段生成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"),括號中對應(yīng)Dao層的路徑,這樣每個Dao接口上面就不用加@Mapper注解了
Service層實現(xiàn)定義
在類名上加上@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
對應(yīng)的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節(jié)點的namespace屬性對應(yīng)Dao層接口路徑轿腺,resultMap節(jié)點的type屬性對應(yīng)實體類對象路徑
<?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文件中進行相關(guān)配置
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 (對應(yīng)dao層接口的路徑)
mybatis.mapper-locations=classpath:mybatis/mapper/*.xml (對應(yīng)MyBatis的xml配置文件的路徑)
一些基礎(chǔ)代碼两嘴,可以通過代碼生成器來完成,這樣基礎(chǔ)的代碼就不用自己再去手動寫了
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);
}
每個實體類對應(yīng)Dao層的mapper都有一個對應(yīng)的實體類的Example仿荆,Service層可以調(diào)用mapper的基本增刪改查方法螺垢,當需要查詢列表數(shù)據(jù)的時候,可以調(diào)用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);
}
}