您的位置:首页 > 编程语言 > Java开发

SpringBoot 整合Mybatis

2017-09-06 11:19 447 查看
pom.xml

<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/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion>
<groupId>s.t.m</groupId>
<artifactId>stm-pro</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>stm-pro</name>
<url>http://maven.apache.org</url>

<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.4.3.RELEASE</version>
</parent>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.7</version>
<scope>test</scope>
</dependency>
<!-- springboot核心包 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-logging</artifactId>
</exclusion>
</exclusions>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>

<!-- 1.4.3不存在,选择了1.3.7 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-log4j</artifactId>
<version>1.3.7.RELEASE</version>
</dependency>

<!-- jdbc模块 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
<exclusions>
<exclusion>
<groupId>org.apache.tomcat</groupId>
<artifactId>tomcat-jdbc</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.0.19</version>
</dependency>

<!-- mysql驱动 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.21</version>
</dependency>

<!-- mybatis模块 -->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.2.0</version>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.7</source>
<target>1.7</target>
<encoding>utf-8</encoding>
</configuration>
</plugin>
</plugins>
</build>
</project>


application.yml

server.port: 10002
spring:
datasource:
driver-class-name: com.mysql.jdbc.Driver
username: db_test
password: db123
url: jdbc:mysql://localhost:3306/kuaizihui_test?useUnicode=true&characterEncoding=utf-8
type: com.alibaba.druid.pool.DruidDataSource
filters: stat
maxActive: 20
initialSize: 1
maxWait: 60000
minIdle: 1
timeBetweenEvictionRunsMillis: 60000
minEvictableIdleTimeMillis: 300000
validationQuery: select 'x'
testWhileIdle: true
testOnBorrow: false
testOnReturn: false
poolPreparedStatements: true
maxOpenPreparedStatements: 20

mybatis:
mapperLocations: s/t/m/dao/*.xml
typeAliasesPackage: s.t.m.domain


GoodsMapper.java

package s.t.m.dao;

import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Repository;

import s.t.m.domain.Goods;

public interface GoodsMapper {
List<Goods> selectGoods();
int saveGoods(Map<String,Object> map);
}


GoodsMapper.xml
<?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="s.t.m.dao.GoodsMapper">
<!-- 因为配置了typeAliasesPackage 可以不用写全路径s.s.m.domain.Goods -->
<select id="selectGoods" resultType="Goods">
SELECT goods_id as 'goodsId',goods_name as 'goodsName' FROM goods_back;
</select>

<insert id="saveGoods" parameterType="java.util.Map">
insert into goods_back(goods_name) values(#{goodsName})
</insert>
</mapper>


SpringBoot启动类

package s.t.m.main;

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.web.servlet.ServletComponentScan;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
@EnableAutoConfiguration
//@ComponentScan(basePackages={"s.t.m.controller","s.t.m.service","s.t.m.dao","s.t.m.interceptor"})
//@ComponentScan(basePackages={"s.t.m.controller","s.t.m.service","s.t.m.dao","s.t.m.startup"})
@ComponentScan(basePackages={"s.t.m.controller","s.t.m.service"})
//@ServletComponentScan(basePackages={"s.t.m.filter","s.t.m.servlet"}) //过滤器、Servlet
//@ServletComponentScan(basePackages={"s.t.m.interceptor"})
@MapperScan("s.t.m.dao")
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}


此时完成整合,可以把Mapper注入到Service进行开发了。。。

-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: