DB数据源之SpringBoot+MyBatis踏坑过程(二)手工配置数据源与加载Mapper.xml扫描
db数据源之springboot+mybatis踏坑过程(二)手工配置数据源与加载mapper.xml扫描
原创文章,版权所有,未经允许进制转载
吐槽之后应该有所改了,该方式可以作为一种过渡方式来使用。
1.环境说明
- 初次使用springboot,时间有限,需要迅速搭建好架构,没有时间研究
- 使用springboot过程中数据源无法获取;
- 使用springboot过程中注解莫名其妙失效;
- 用springboot过程中因为版本不懂,不扫描application.properties;
- 使用springboot过程中因为版本不懂,不扫描mybatis的mapper.xml包;
- springboot或spring注解使用十分不习惯或者还没有来得及深入学习的情况;
假设有以上问题,又时间紧迫,建议使用本文手工配置方式!!
springboot,parent 2.0.2.和1.5.3.都已经测试过,
在java8和java7环境下测试过。前者配java8,后者配java7,
使用myeclipse 2017 c1 64x,myeclipse 2016之前的版本无法使用java8
pom.xml核心如下:
<parent> <groupid>org.springframework.boot</groupid> <artifactid>spring-boot-starter-parent</artifactid> <version>2.0.2.release</version> <relativepath /> <!-- lookup parent from repository --> </parent> <dependencies> <!-- 添加mysql依赖 --> <dependency> <groupid>mysql</groupid> <artifactid>mysql-connector-java</artifactid> </dependency> <!-- 添加jdbc依赖 --> <dependency> <groupid>org.springframework.boot</groupid> <artifactid>spring-boot-starter-jdbc</artifactid> </dependency> <!-- mybaits基础依赖 --> <dependency> <groupid>org.mybatis</groupid> <artifactid>mybatis</artifactid> <version>3.4.0</version> </dependency> <!-- mybatis插件依赖 --> <dependency> <groupid>org.mybatis.spring.boot</groupid> <artifactid>mybatis-spring-boot-starter</artifactid> <version>1.1.1</version> </dependency> <!-- mapper依赖 --> <dependency> <groupid>tk.mybatis</groupid> <artifactid>mapper</artifactid> <version>3.3.7</version> </dependency> <dependency> <groupid>org.springframework.boot</groupid> <artifactid>spring-boot-configuration-processor</artifactid> <optional>true</optional> </dependency>
2.配置思路
2.1.手工获取application.properties文件中的属性;
2.2.创建数据源datasource;
2.3.注入数据源属性;
2.4.创建sqlsessionfactory;
2.5.sqlsessionfactory配置datasource;
2.6.sqlsessionfactory配置扫描mybatis-config.xml文件;
2.7.sqlsessionfactory配置扫描mapper.xml所在包;
2.8.获取session查询数据库进行测试;
3.所需类与结构
3.0.application.properties文件与相应内容作为数据源;
3.1.sysconfig类,用于获取application.properties中的property;
3.2.dataconfig类,用于获取sqlsessionfactory;
3.3.examplecontroller类,用于测试;
3.4.apprun类,springboot的启动入口,将dataconfig初始化;
3.5.mapper.xml内容
4.代码
4.0.application.properties部分内容段落:
1 master.url=jdbc:mysql://47.105.121.158:3306/master?characterencoding=utf8 2 master.username=root 3 master.password=root 4 master.driver=com.mysql.jdbc.driver 5 #master.driver-class-name=com.mysql.jdbc.driver 一般是使用这个命名模式
4.1.sysconfig类,代码如下:
1 package com.fm.config; 2 3 import java.io.ioexception; 4 import java.util.properties; 5 6 import org.springframework.context.annotation.configuration; 7 import org.springframework.core.io.classpathresource; 8 import org.springframework.core.io.resource; 9 import org.springframework.core.io.support.propertiesloaderutils; 10 11 /** 12 * 用于读取properties的类,基础配置文件名为application.properties,置于resources根目录下 13 * @author liuyuhang 14 */ 15 public class sysconfig { 16 17 private properties properties; 18 19 /** 20 * 修改无参构造,默认该类实例化的时候,加载配置文件中的内容,不做单例,因为配置文件可能更改 21 */ 22 public sysconfig() { 23 try { 24 resource resource = new classpathresource("/application.properties"); 25 properties = propertiesloaderutils.loadproperties(resource); 26 } catch (ioexception e) { 27 e.printstacktrace(); 28 } 29 } 30 31 /** 32 * 获取属性,传入参数key 33 */ 34 public string getproperty(string key) { 35 return properties.getproperty(key); 36 } 37 }
4.2.datasourceconfig类,代码如下:
1 package com.fm.config; 2 3 import java.util.hashmap; 4 5 import javax.sql.datasource; 6 7 import org.apache.ibatis.session.sqlsessionfactory; 8 import org.mybatis.spring.sqlsessionfactorybean; 9 import org.springframework.boot.jdbc.datasourcebuilder; 10 import org.springframework.core.io.defaultresourceloader; 11 import org.springframework.core.io.resource; 12 import org.springframework.core.io.support.pathmatchingresourcepatternresolver; 13 /** 14 * dataconfig,获取数据源,配置给sqlsessionfactory,并以此获取session 15 * 16 * @author liuyuhang 17 */ 18 public class dataconfig { 19 /** 20 * 缓存factory的map,作为单例sessionfactory存储 21 */ 22 public static hashmap<string, sqlsessionfactory> factorymap = new hashmap<string, sqlsessionfactory>(); 23 24 /** 25 * 构造器对缓存中的factory只实例化一次 26 * 不保证该单例能顺利执行,若看出问题,自行更改 27 * @throws exception 28 */ 29 public dataconfig() { 30 system.out.println("out init sessionfactory:" + factorymap); 31 if (factorymap.isempty()) { 32 synchronized (factorymap) { 33 if (factorymap.isempty()) { 34 try { 35 sqlsessionfactory sessionfactory = getsessionfactory(); 36 factorymap.put("master", sessionfactory); 37 system.out.println("in init sessionfactory:" + factorymap); 38 } catch (exception e) { 39 system.out.println("该错误比较严重,出现在数据源无参构造函数中!!"); 40 e.printstacktrace(); 41 } 42 43 } 44 } 45 46 } 47 48 } 49 50 /** 51 * 手动获取sessionfactory用例 52 * @param datasourceperfix 53 * @return 54 * @throws exception 55 */ 56 public sqlsessionfactory getsessionfactory() throws exception { 57 sysconfig sc = new sysconfig(); 58 string masterurl = sc.getproperty("master.url"); 59 string masterdriver = sc.getproperty("master.driver"); 60 string masterusername = sc.getproperty("master.username"); 61 string masterpassword = sc.getproperty("master.password"); 62 // 创建数据源 63 datasourcebuilder create = datasourcebuilder.create(); 64 create.url(masterurl); 65 create.driverclassname(masterdriver); 66 create.username(masterusername); 67 create.password(masterpassword); 68 datasource source = create.build(); 69 // 创建sessionfactory 70 sqlsessionfactorybean factorybean = new sqlsessionfactorybean(); 71 factorybean.setdatasource(source);// 加载数据源 72 // 扫描mapper.xml 73 resource[] resources = new pathmatchingresourcepatternresolver().getresources("classpath:com/fm/mapper/*.xml"); 74 factorybean.setmapperlocations(resources); 75 // 读取config 76 factorybean.setconfiglocation(new defaultresourceloader().getresource("classpath:mybatis-config.xml")); 77 sqlsessionfactory sessionfactory = factorybean.getobject(); 78 return sessionfactory; 79 } 80 81 }
4.3.examplecontroller类,代码如下:
1 package com.fm.controller; 2 3 import java.util.hashmap; 4 import java.util.list; 5 import java.util.map; 6 7 import javax.servlet.http.httpservletrequest; 8 9 import org.apache.ibatis.session.sqlsession; 10 import org.apache.ibatis.session.sqlsessionfactory; 11 import org.springframework.web.bind.annotation.requestmapping; 12 import org.springframework.web.bind.annotation.restcontroller; 13 14 import com.fm.config.dataconfig; 15 16 /** 17 * controler用于测试 18 * @author liuyuhang 19 */ 20 @restcontroller //等同于responsebody + controller双重注解 21 public class examplecontroller { 22 23 /** 24 * 手动创建session查询数据库用例,该方法可以创建多个sessionfactory,用多线程 25 * @param request 26 * @return 27 * @throws exception 28 */ 29 @requestmapping("/hellomybatis") 30 public list hellomybatis(httpservletrequest request) throws exception { 31 //数据源配置无参构造器 32 dataconfig dc = new dataconfig(); 33 sqlsessionfactory sessionfactory = dc.getsessionfactory();//获取sessionfactory 34 sqlsession session = sessionfactory.opensession();//获取session 35 list<object> selectlist = session.selectlist("com.fm.mapper.mysqlmapper.getuser"); 36 return selectlist;//自动转换为json 37 } 38 }
4.4.apprun类,代码如下:
1 package com.fm; 2 3 import org.springframework.boot.springapplication; 4 import org.springframework.boot.springbootconfiguration; 5 import org.springframework.boot.autoconfigure.springbootapplication; 6 import org.springframework.boot.autoconfigure.jdbc.datasourceautoconfiguration; 7 import org.springframework.boot.web.servlet.servletcomponentscan; 8 9 import com.fm.config.dataconfig; 10 11 @springbootapplication(exclude = { datasourceautoconfiguration.class }) // 禁用默认的单数据源配置 12 @springbootconfiguration // springboot基础配置注解 13 @servletcomponentscan // springboot servlet filter 14 // @enableconfigurationproperties//该注解于springboot1.5以上废弃 15 public class apprun { 16 17 public static void main(string[] args) throws exception { 18 springapplication.run(apprun.class, args); 19 dataconfig dc = new dataconfig();//初始化配置 20 21 } 22 }
4.5.mapper.xml内容
1 <?xml version="1.0" encoding="utf-8" ?> 2 <!doctype mapper public "-//mybatis.org//dtd mapper 3.0//en" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" > 3 <mapper namespace="com.fm.mapper.mysqlmapper"> 4 <!-- 随便写作为测试而已 --> 5 <resultmap id="getusermap" type="java.util.map"> 6 <result column="id" property="id" jdbctype="integer" javatype="int" /> 7 <result column="username" property="username" jdbctype="varchar" javatype="string" />< 8 <result column="password" property="password" jdbctype="varchar" javatype="string" /> 9 </resultmap> 10 <select id="getuser" parametertype="java.util.map" resultmap="getusermap"> 11 select * from user 12 </select> 13 </mapper>
5.测试
启动后控制台显示如下:
浏览器输入 http://localhost:8080/hellomybatis
控制台结果如下图:
页面结果如下图:
6.总结
spring注解一直是我饿心结,当我想将我的代码改成以注解方式来进行装配注入的时候,总是不行的,
于是乎我学会了很多奇葩的手段,可能不主流。
吐槽归吐槽,学习归学习,工作归工作,一码是一码!!!
对于以上配置过程的springboot的注解版,明日再更
休息!
以上!