springboot mybatis配置
Posted 十一路客
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了springboot mybatis配置相关的知识,希望对你有一定的参考价值。
1.application.properties配置数据库地址
#####database
app.myproject.db.driverClassName=com.mysql.jdbc.Driver
app.myproject.db.initialSize=10
app.myproject.db.maxActive=99
app.myproject.db.minIdle=9
app.myproject.db.maxIdle=20
app.myproject.db.removeAbandoned=true
app.myproject.db.removeAbandonedTimeout=180
app.myproject.db.maxWait=60000
app.myproject.db.validationQuery=select 1 from dual
#from dual
app.myproject.db.testOnBorrow=true
app.myproject.db.connectionProperties=clientEncoding=UTF-8
#test database
app.myproject.db.url=jdbc:mysql://XXX.XXX.XXX.XX:3306/databaseName?useUnicode=true&characterEncoding=utf-8
app.myproject.db.username=manager
#manager
app.myproject.db.password=Abc$1234
2.XML配置数据源
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<bean id="dataSource" class="org.apache.tomcat.jdbc.pool.DataSource"
destroy-method="close">
<property name="url" value="$app.myproject.db.url" />
<property name="username" value="$app.myproject.db.username" />
<property name="password" value="$app.myproject.db.password" />
<property name="driverClassName" value="$app.myproject.db.driverClassName" />
<property name="initialSize" value="$app.myproject.db.initialSize" />
<property name="maxActive" value="$app.myproject.db.maxActive" />
<property name="minIdle" value="$app.myproject.db.minIdle" />
<property name="maxIdle" value="$app.myproject.db.maxIdle" />
<property name="removeAbandoned" value="$app.myproject.db.removeAbandoned" />
<property name="removeAbandonedTimeout" value="$app.myproject.db.removeAbandonedTimeout" />
<property name="maxWait" value="$app.myproject.db.maxWait" />
<property name="validationQuery" value="$app.myproject.db.validationQuery" />
<property name="testOnBorrow" value="$app.myproject.db.testOnBorrow" />
</bean>
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="configLocation" value="mybatis/mybatis-config.xml" />
</bean>
<bean id="apiMapper" class="org.mybatis.spring.mapper.MapperFactoryBean">
<property name="mapperInterface"
value="com.myproject.persist.mapper.ApiMapper" />
<property name="sqlSessionFactory" ref="sqlSessionFactory" />
</bean>
</beans>
3.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" >
<configuration>
<typeAliases>
<typeAlias type="com.myproject.domain.UserInfo" alias="UserInfo" />
<typeAlias type="com.myproject.domain.TestProject" alias="TestProject" />
</typeAliases>
<mappers>
<mapper resource="com/myproject/persist/mapper/ApiMapper.xml" />
</mappers>
</configuration>
4.ApiMapper.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="com.myproject.persist.mapper.ApiMapper">
<select id="" parameterType="java.util.HashMap" resultType="TestProject">
</select>
<select id="" parameterType="java.util.HashMap" resultMap="TestProjectMap">
</select>
</mapper>
以上是关于springboot mybatis配置的主要内容,如果未能解决你的问题,请参考以下文章
SpringBoot + mybatis + druid 配置两个数据源
springboot-mybatis配置(xml)/springboot-jpa配置
springboot+mybatis+Druid配置多数据源(mysql+postgre)