SpringBoot 整合Mybatis
2017-09-06 11:19
447 查看
pom.xml
application.yml
GoodsMapper.java
GoodsMapper.xml
SpringBoot启动类
此时完成整合,可以把Mapper注入到Service进行开发了。。。
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
<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进行开发了。。。
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
相关文章推荐
- spring boot + mybatis + jsp整合
- springboot整合mybatis使用阿里(阿里连接池)和xml方式
- springboot整合mybatis(xml版)
- springboot整合多个mybatis数据源
- Springboot 整合 Mybatis 的完整 Web 案例
- springboot整合Mybatis
- Spring boot+mybatis整合
- Spring boot整合mybatis
- 开发框架整合与搭建:spring boot+mybatis+jedis
- Spring Boot + MyBatis + MySQL 整合(1)
- SpringBoot整合Mybatis
- SpringBoot整合mybatis、shiro、redis实现基于数据库的细粒度动态权限管理系统实例
- SpringBoot整合Mybatis之进门篇
- springboot整合mybatis,及其事物配置
- (1)、Spring Boot与Mybatis的整合(单数据源)
- SpringBoot非官方教程 | 第六篇:springboot整合mybatis
- Spring Boot整合MyBatis
- Spring-Boot学习笔记-整合Mybatis-Druid-PageHelper
- SpringBoot+Mybatis整合报,service注入失败,dao注入失败,
- springboot(3)整合mybatis