Spring boot集成Mybatis的方式有两种,一种是将DAO下的依旧放在src下,另一种则是将DAO下的.xml文件放到资源路径下面。
首先在pom.xml中加入mysql和mybatis整合Springboot框架的起步依赖:
<!--mysql依赖-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<!--mybatis整合Springboot框架的起步依赖-->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.0.0</version>
</dependency>
以第一种为例,先写出web(controller)层:
package com.study.demo.web;
import com.study.demo.model.Books;
import com.study.demo.service.BooksService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
@Controller
public class IndexController {
@Autowired
private BooksService booksService;
@GetMapping("/books")
public @ResponseBody Object books(Integer id){
Books books = booksService.selectByPrimaryKey(id);
return books;
}
}
@Controller
注解将IndexController类配置到spring容器中,使用@Autowired
注解,直接调用service层。
service层:(这里为查询功能一种方法测试)
package com.study.demo.service;
import com.study.demo.model.Books;
public interface BooksService {
Books selectByPrimaryKey(Integer bookid);
}
实现类:
package com.study.demo.service.impl;
import com.study.demo.mapper.BooksMapper;
import com.study.demo.model.Books;
import com.study.demo.service.BooksService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class BooksServiceImpl implements BooksService {
@Autowired
private BooksMapper booksMapper;
@Override
public Books selectByPrimaryKey(Integer bookid) {
return booksMapper.selectByPrimaryKey(bookid);
}
}
同样用@Service
注解,将工具类申明为spring组件。
mapper(DAO)层:
package com.study.demo.mapper;
import com.study.demo.model.Books;
import org.apache.ibatis.annotations.Mapper;
//@Mapper //扫描DAO接口到spring容器
public interface BooksMapper {
int deleteByPrimaryKey(Integer bookid);
int insert(Books record);
int insertSelective(Books record);
Books selectByPrimaryKey(Integer bookid);
int updateByPrimaryKeySelective(Books record);
int updateByPrimaryKey(Books record);
}
这里在接口类的上面可以使用@Mapper
注解,将接口类申明为spring组件。也可以不在这里加入注释,而在程序主入口出加入注解@MapperScan(basePackages = "***")
,此注解用来开启扫描Mapper接口的包以及子目录。
而mapper.xml文件则是通过mybatis逆向工程实现的代码:
(MyBatis逆向工程代码的生成:https://blog.csdn.net/qq_45829231/article/details/117263141)
<?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.study.demo.mapper.BooksMapper">
<resultMap id="BaseResultMap" type="com.study.demo.model.Books">
<id column="bookID" jdbcType="INTEGER" property="bookid" />
<result column="bookName" jdbcType="VARCHAR" property="bookname" />
<result column="bookCounts" jdbcType="INTEGER" property="bookcounts" />
<result column="detail" jdbcType="VARCHAR" property="detail" />
</resultMap>
<sql id="Base_Column_List">
bookID, bookName, bookCounts, detail
</sql>
<select id="selectByPrimaryKey" parameterType="java.lang.Integer" resultMap="BaseResultMap">
select
<include refid="Base_Column_List" />
from books
where bookID = #{bookid,jdbcType=INTEGER}
</select>
<delete id="deleteByPrimaryKey" parameterType="java.lang.Integer">
delete from books
where bookID = #{bookid,jdbcType=INTEGER}
</delete>
<insert id="insert" parameterType="com.study.demo.model.Books">
insert into books (bookID, bookName, bookCounts,
detail)
values (#{bookid,jdbcType=INTEGER}, #{bookname,jdbcType=VARCHAR}, #{bookcounts,jdbcType=INTEGER},
#{detail,jdbcType=VARCHAR})
</insert>
<insert id="insertSelective" parameterType="com.study.demo.model.Books">
insert into books
<trim prefix="(" suffix=")" suffixOverrides=",">
<if test="bookid != null">
bookID,
</if>
<if test="bookname != null">
bookName,
</if>
<if test="bookcounts != null">
bookCounts,
</if>
<if test="detail != null">
detail,
</if>
</trim>
<trim prefix="values (" suffix=")" suffixOverrides=",">
<if test="bookid != null">
#{bookid,jdbcType=INTEGER},
</if>
<if test="bookname != null">
#{bookname,jdbcType=VARCHAR},
</if>
<if test="bookcounts != null">
#{bookcounts,jdbcType=INTEGER},
</if>
<if test="detail != null">
#{detail,jdbcType=VARCHAR},
</if>
</trim>
</insert>
<update id="updateByPrimaryKeySelective" parameterType="com.study.demo.model.Books">
update books
<set>
<if test="bookname != null">
bookName = #{bookname,jdbcType=VARCHAR},
</if>
<if test="bookcounts != null">
bookCounts = #{bookcounts,jdbcType=INTEGER},
</if>
<if test="detail != null">
detail = #{detail,jdbcType=VARCHAR},
</if>
</set>
where bookID = #{bookid,jdbcType=INTEGER}
</update>
<update id="updateByPrimaryKey" parameterType="com.study.demo.model.Books">
update books
set bookName = #{bookname,jdbcType=VARCHAR},
bookCounts = #{bookcounts,jdbcType=INTEGER},
detail = #{detail,jdbcType=VARCHAR}
where bookID = #{bookid,jdbcType=INTEGER}
</update>
</mapper>
最后在资源路径下的application.properties主配置文件中配置连接数据库的配置:
#设置连接数据库配置
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/ssmbuild
spring.datasource.username=root
spring.datasource.password=0010
测试成功:
第二种方式和第一种大体类似,只是将mapper下的xml文件放入到资源文件下:
这种方式需要在著配置文件中加入:
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/ssmbuild
spring.datasource.password=0010
spring.datasource.username=root
#指定Mybatis映射文件的路径
mybatis.mapper-locations=classpath:mapper/*.xml
即可。而绝大多数的公司都会选择使用第二种方式,因此需要学会使用。