项目的很简单没有多少代码,具体的使用的需要详细说明,后面我会更新的mybatis的使用手册。
依赖引入
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jdbc</artifactId>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.1.4</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
配置参数
#数据库配置
spring.datasource.username=root
spring.datasource.password=123456
spring.datasource.url=jdbc:mysql://127.0.0.1/spring_boot_test?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=UTC
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
#mybatis配置
#mapper.xml文件路径
mybatis.mapper-locations=classpath:mapper/*.xml
#对应DTO包
mybatis.type-aliases-package=com.mybatis.demo.entity
DTO实体类
package com.mybatis.demo.entity;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.Getter;
import lombok.Setter;
import java.util.Date;
@Data
@Setter
@Getter
@AllArgsConstructor
public class Mmp {
private Integer no;
private String name;
}
Mapper接口
package com.mybatis.demo.mapper;
import com.mybatis.demo.entity.Mmp;
import org.apache.ibatis.annotations.Mapper;
import org.springframework.stereotype.Repository;
import java.util.List;
@Repository
@Mapper
public interface MmpMapper {
List<Mmp> selectMmp();
Mmp selectMmpById(Integer no);
Integer addMmp(Mmp mmp);
Integer updateMmp(Mmp mmp);
Integer deleteMmp(Integer no);
}
MappeXML文件
<?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.mybatis.demo.mapper.MmpMapper">
<select id="selectMmp" resultType="Mmp">
select * from mmp
</select>
<select id="selectMmpById" resultType="Mmp">
select * from mmp where no = #{no}
</select>
<insert id="addMmp" parameterType="Mmp">
insert into mmp (no,name) values (#{no},#{name})
</insert>
<update id="updateMmp" parameterType="Mmp">
update mmp set name=#{name} where no = #{no}
</update>
<delete id="deleteMmp" parameterType="int">
delete from mmp where no = #{no}
</delete>
</mapper>
控制层
实际项目中的Mapper的调用都是Sevrice层,控制层调用Sevrice,例子代码比较简单,调用也只是说明效果。
package com.mybatis.demo.controller;
import com.mybatis.demo.entity.Mmp;
import com.mybatis.demo.mapper.MmpMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
@RestController
public class TestController {
@Autowired
private MmpMapper mmpMapper;
//选择全部用户
@GetMapping("/selectMmp")
public String selectMmp(){
List<Mmp> mmps = mmpMapper.selectMmp();
for (Mmp mmp : mmps) {
System.out.println(mmp);
}
return "ok";
}
//根据id选择用户
@GetMapping("/selectMmpById")
public String selectMmpById(){
Mmp mmp = mmpMapper.selectMmpById(1234);
System.out.println(mmp);
return "ok";
}
//添加一个用户
@GetMapping("/addMmp")
public String addMmp(){
mmpMapper.addMmp(new Mmp(1234,"heheda"));
return "ok";
}
//修改一个用户
@GetMapping("/updateMmp")
public String updateMmp(){
mmpMapper.updateMmp(new Mmp(1234,"heihei"));
return "ok";
}
//根据id删除用户
@GetMapping("/deleteMmp")
public String deleteMmp(){
mmpMapper.deleteMmp(1234);
return "ok";
}
}
代码地址
https://gitee.com/zhang798/spring-demo.git
分支 springBoot_mybatis
git clone https://gitee.com/zhang798/spring-demo.git -b springBoot_mybatis