SpringBoot整合Mybatis
2018-02-01 17:04
369 查看
SpringBoot如何优雅的整合Mybatis?
1.POM配置文件
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion>
<groupId>com.lgy</groupId>
<artifactId>spring-boot-first</artifactId>
<packaging>jar</packaging>
<version>1.0-SNAPSHOT</version>
<name>spring-boot-first Maven Webapp</name>
<url>http://maven.apache.org</url>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.9.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.alibaba/fastjson -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.38</version>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.1.1</version>
</dependency>
<!--mysql-connector-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
<finalName>spring-boot-first</finalName>
</build>
</project>
主要是mybatis-spring-boot-starter,mysql-connector-java。
2.Mybasts配置文件
server.port=8001
mybatis.type-aliases-package=com.lgy.entity
spring.datasource.driverClassName = com.mysql.jdbc.Driver
spring.datasource.url = jdbc:mysql://10.202.203.70:3306/test?useUnicode=true&characterEncoding=utf-8
spring.datasource.username = root
spring.datasource.password = root
# mybatis xml
mybatis.config-locations=classpath:config/mybatis-config.xml
mybatis.mapper-locations=classpath:mappers/*Mapper.xmla.配置bean包扫描别名
b.配置datasource数据源
c.配置mybatis配置文件
mybatis-config.xml文件中的内容
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd">
<!--通过这个配置文件,完成mybatis与数据库的连接 -->
<configuration>
<typeAliases>
<typeAlias alias="Integer" type="java.lang.Integer" />
<typeAlias alias="String" type="java.lang.String" />
<typeAlias alias="Long" type="java.lang.Long" />
<typeAlias alias="HashMap" type="java.util.HashMap" />
<typeAlias alias="LinkedHashMap" type="java.util.LinkedHashMap" />
<typeAlias alias="ArrayList" type="java.util.ArrayList" />
<typeAlias alias="LinkedList" type="java.util.LinkedList" />
</typeAliases>
</configuration>
@SpringBootApplication
@MapperScan("com.lgy.dao")
public class AppMain {
public static void main(String[] args) {
SpringApplication.run(AppMain.class, args);
}
}
***Mapper扫描早项目启动类中进行!
3.Mybatis的基本写法
package com.lgy.entity;
/**
* Created by fengch on 2018/2/1.
*/
public class Test {
private int id;
private String name;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
分别是entity,dao,mapper
4.测试
package com.lgy.dao;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
/**
* Created by fengch on 2018/2/1.
*/
@RunWith(SpringRunner.class)
@SpringBootTest
public class TestDao {
@Autowired
private TestMapper testMapper;
@Test
public void test() throws Exception {
com.lgy.entity.Test t = testMapper.selectByName("aaa");
System.out.println(t.getId() + ":::" + t.getName());
}
}
是不是很easy!
1.POM配置文件
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion>
<groupId>com.lgy</groupId>
<artifactId>spring-boot-first</artifactId>
<packaging>jar</packaging>
<version>1.0-SNAPSHOT</version>
<name>spring-boot-first Maven Webapp</name>
<url>http://maven.apache.org</url>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.9.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.alibaba/fastjson -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.38</version>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.1.1</version>
</dependency>
<!--mysql-connector-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
<finalName>spring-boot-first</finalName>
</build>
</project>
主要是mybatis-spring-boot-starter,mysql-connector-java。
2.Mybasts配置文件
server.port=8001
mybatis.type-aliases-package=com.lgy.entity
spring.datasource.driverClassName = com.mysql.jdbc.Driver
spring.datasource.url = jdbc:mysql://10.202.203.70:3306/test?useUnicode=true&characterEncoding=utf-8
spring.datasource.username = root
spring.datasource.password = root
# mybatis xml
mybatis.config-locations=classpath:config/mybatis-config.xml
mybatis.mapper-locations=classpath:mappers/*Mapper.xmla.配置bean包扫描别名
b.配置datasource数据源
c.配置mybatis配置文件
mybatis-config.xml文件中的内容
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd">
<!--通过这个配置文件,完成mybatis与数据库的连接 -->
<configuration>
<typeAliases>
<typeAlias alias="Integer" type="java.lang.Integer" />
<typeAlias alias="String" type="java.lang.String" />
<typeAlias alias="Long" type="java.lang.Long" />
<typeAlias alias="HashMap" type="java.util.HashMap" />
<typeAlias alias="LinkedHashMap" type="java.util.LinkedHashMap" />
<typeAlias alias="ArrayList" type="java.util.ArrayList" />
<typeAlias alias="LinkedList" type="java.util.LinkedList" />
</typeAliases>
</configuration>
@SpringBootApplication
@MapperScan("com.lgy.dao")
public class AppMain {
public static void main(String[] args) {
SpringApplication.run(AppMain.class, args);
}
}
***Mapper扫描早项目启动类中进行!
3.Mybatis的基本写法
package com.lgy.entity;
/**
* Created by fengch on 2018/2/1.
*/
public class Test {
private int id;
private String name;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
package com.lgy.dao; import com.lgy.entity.Test; /** * Created by fengch on 2018/2/1. */ public interface TestMapper { Test selectByName(String name); }
<?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.lgy.dao.TestMapper"> ba9e <select id="selectByName" parameterType="String" resultType="test"> SELECT * FROM test_test0 WHERE `name` = #{name} </select> </mapper>
分别是entity,dao,mapper
4.测试
package com.lgy.dao;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
/**
* Created by fengch on 2018/2/1.
*/
@RunWith(SpringRunner.class)
@SpringBootTest
public class TestDao {
@Autowired
private TestMapper testMapper;
@Test
public void test() throws Exception {
com.lgy.entity.Test t = testMapper.selectByName("aaa");
System.out.println(t.getId() + ":::" + t.getName());
}
}
是不是很easy!
相关文章推荐
- springboot整合mybatis
- springboot-整合mybatis
- Spring Boot + Kotlin整合MyBatis的方法教程
- spring-boot | 使员Druid 整合Mybatis 最简配置多数据源
- Spring Boot系列之七 以xml整合MyBatis
- spring boot整合mybatis+mybatis-plus
- springboot+springmvc+mybatis项目整合
- springboot整合mybatis
- springboot与mybatis整合实例详解
- Spring Boot整合MyBatis
- Spring boot Mybatis 整合(完整版)
- Spring boot整合mybatis
- Mybatis+Spring boot整合趟坑————自定义TypeHandler
- SpringBoot学习(五)——整合MyBatis
- Spring boot + mysql +mybatis 配置整合实例
- springboot与mybatis整合dao层不能注入的问题
- spring boot 2.0.1整合MyBatis
- SpringBoot入门——整合Mybatis
- Spring Boot整合MyBatis连接Oracle数据库的步骤全纪录
- Springboot整合mybatis的坑