1. 程式人生 > >SpringBoot整合Mybatis(使用配置檔案方式)

SpringBoot整合Mybatis(使用配置檔案方式)

1.首先看下一下maven的依賴,主要是mybatis依賴,mysql驅動,以及之後測試使用的依賴spring-boot-test

        <!--mybatis依賴包-->
		<dependency>
			<groupId>org.mybatis.spring.boot</groupId>
			<artifactId>mybatis-spring-boot-starter</artifactId>
			<version>1.3.2</version>
		</dependency>
		<!--Mysql包-->
		<dependency>
			<groupId>mysql</groupId>
			<artifactId>mysql-connector-java</artifactId>
		</dependency>
		<dependency>
			<groupId>org.junit.jupiter</groupId>
			<artifactId>junit-jupiter-api</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-test</artifactId>
			<version>5.1.2.RELEASE</version>
			<scope>compile</scope>
		</dependency>
		<dependency>
			<groupId>junit</groupId>
			<artifactId>junit</artifactId>
			<version>4.12</version>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-test</artifactId>
		</dependency>

2.其次在application.properties檔案中配置mybatis的mapper檔案位置,和實體類的包路徑,還有最好加上駝峰命名規範


####mybatis配置
# 注意注意
mybatis.mapper-locations=classpath:edu/hohai/chapter1/gt/mapper/*.xml
#mybatis.mapper-locations=classpath:mapper/*.xml   #這種方式需要自己在resources目錄下建立mapper目錄然後存放xml
mybatis.type-aliases-package=edu.hohai.chapter1.gt.entity
# 駝峰命名規範 如:資料庫欄位是  order_id 那麼 實體欄位就要寫成 orderId
mybatis.configuration.map-underscore-to-camel-case=true

看下專案結構:這裡我把Mapper類和Mapper對映檔案放在了一起,這時候我們還需要做下面設定,讓springboot能在java包中掃描xml檔案(springboot預設掃描的是包中的*.java檔案),如果使用resources目錄下面的xml,這開啟上面的

mybatis.mapper-locations=classpath:mapper/*.xml

專案結構圖

3.設定springboot在包中掃描xml檔案

<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
				<configuration>
					<fork>true</fork>
				</configuration>
			</plugin>
		</plugins>

		<!--下面主要是設定在java包裡面也讓springboot掃描xml型別的配置檔案-->
		<resources>
			<resource>
				<directory>src/main/resources</directory>
			</resource>
			<resource>
				<directory>src/main/java</directory>
				<includes>
					<include>**/*.xml</include>
				</includes>
				<filtering>true</filtering>
			</resource>
		</resources>
</build>

4.UserMapper.java  UserMapper.xml

package edu.hohai.chapter1.gt.mapper;

import edu.hohai.chapter1.gt.entity.User;
import org.apache.ibatis.annotations.Mapper;

/**
 * @author cronous
 * @create 2018-11-01 15:04
 */
@Mapper //注意這裡的Mapper註解
public interface UserMapper {

    int insert(User user);
}
<?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="edu.hohai.chapter1.gt.mapper.UserMapper">

    <insert id="insert" parameterType="edu.hohai.chapter1.gt.entity.User">
    INSERT INTO `t_user`(`username`,`password`) VALUES (#{username},#{password})
  </insert>

</mapper>

5.資料庫表

CREATE TABLE `t_user` (
  `id` int(8) NOT NULL AUTO_INCREMENT COMMENT '主鍵自增',
  `username` varchar(50) NOT NULL COMMENT '使用者名稱',
  `password` varchar(50) NOT NULL COMMENT '密碼',
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=6 DEFAULT CHARSET=utf8 COMMENT='使用者表';

6.測試

package edu.hohai.chapter1;

import edu.hohai.chapter1.gt.entity.User;
import edu.hohai.chapter1.gt.mapper.UserMapper;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;


/**
 * @author cronous
 * @create 2018-11-01 15:20
 */
@RunWith(SpringRunner.class)
@SpringBootTest
public class ChapterTest {

    private static final Logger log = LoggerFactory.getLogger(ChapterTest.class);

    @Autowired
    private UserMapper userMapper;

    @Test
    public void test1() throws Exception {


        final int row1 = userMapper.insert(new User("u1", "p1"));
        log.info("[新增結果] - [{}]", row1);
        final int row2 = userMapper.insert(new User("u2", "p2"));
        log.info("[新增結果] - [{}]", row2);
        final int row3 = userMapper.insert(new User("u1", "p3"));
        log.info("[新增結果] - [{}]", row3);


    }
}

7.檢視資料庫

發現已經寫入資料庫,測試完成