SpringBoot 配置 MyBatis


MyBatis

新建工程,選好資源


實(shí)體類

不需要添加各種注解,直接把對(duì)應(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();

}

注:如果這里不想每個(gè)接口都加注解的話,可以在SpringBoot啟動(dòng)類上面加上注解@MapperScan("com.bruce.SpringBootMVC04Mybatis.mapper"),括號(hào)中對(duì)應(yīng)Dao層的路徑,這樣每個(gè)Dao接口上面就不用加@Mapper注解了

Service層實(shí)現(xiàn)定義

在類名上加上@Service注解,在注入的Dao接口對(duì)象上加上@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對(duì)象加上注解@Autowired
對(duì)應(yīng)的Controller方法上加上@RequestMapping注解來配置請(qǐng)求路徑

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é)點(diǎn)的namespace屬性對(duì)應(yīng)Dao層接口路徑,resultMap節(jié)點(diǎn)的type屬性對(duì)應(yīng)實(shí)體類對(duì)象路徑

<?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文件中進(jìn)行相關(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 (對(duì)應(yīng)dao層接口的路徑)

mybatis.mapper-locations=classpath:mybatis/mapper/*.xml (對(duì)應(yīng)MyBatis的xml配置文件的路徑)


一些基礎(chǔ)代碼,可以通過代碼生成器來完成,這樣基礎(chǔ)的代碼就不用自己再去手動(dòng)寫了

mybatis-generator-gui 使用說明與下載地址:https://github.com/zouzg/mybatis-generator-gui

生成的代碼中Dao層的基類MyBatisBaseDao包含了所有的基本方法

/**

* DAO公共基類,由MybatisGenerator自動(dòng)生成請(qǐng)勿修改

* @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);

}


每個(gè)實(shí)體類對(duì)應(yīng)Dao層的mapper都有一個(gè)對(duì)應(yīng)的實(shí)體類的Example,Service層可以調(diào)用mapper的基本增刪改查方法,當(dāng)需要查詢列表數(shù)據(jù)的時(shí)候,可以調(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進(jìn)行分頁操作

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>

需要加入分頁的方法,可以如下進(jìn)行修改

@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);

}

}

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容