创建一个Spring集成MyBatis的项目
Posted 肖帆咪
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了创建一个Spring集成MyBatis的项目相关的知识,希望对你有一定的参考价值。
选择java EE创建一个项目
项目目录
config.properties文件关于数据库的相关数据
driver=com.mysql.cj.jdbc.Driver
url=jdbc:mysql://localhost:3306/mybatis_db?characterEncoding=utf8&serverTimezone=Asia/Shanghai
uname=root
password=root
db.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"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
">
<!--导入属性文件-->
<context:property-placeholder location="config.properties"></context:property-placeholder>
<!--创建阿里 提供的数据库连接对象管理类 数据库连接池-->
<bean class="com.alibaba.druid.pool.DruidDataSource" name="dataSource">
<property name="driverClassName" value="$driver"></property>
<property name="url" value="$url"></property>
<property name="username" value="$uname"></property>
<property name="password" value="$password"></property>
<property name="initialSize" value="10"></property>
<property name="minIdle" value="5"></property>
<property name="maxActive" value="20"></property>
</bean>
<!--配置spring提供的事务管理类-->
<bean class="org.springframework.jdbc.datasource.DataSourceTransactionManager" id="transactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean>
<!--开启spring注解事务-->
<tx:annotation-driven transaction-manager="transactionManager"></tx:annotation-driven>
</beans>
mybatis.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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans.xsd">
<import resource="db.xml"></import>
<!--spring管理类sqlSessionFanctory对象的创建-->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<property name="configLocation" value="myBatis-configer.xml"></property>
<property name="mapperLocations" value="mapper/*Mapper.xml">
</property>
</bean>
<!--扫描dao接口,生成接口的代理对象-->
<bean id="mapperFactory" class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.springbatis.dao"></property>
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory">
</property>
</bean>
</beans>
myBatis-configer.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>
<properties resource="config.properties"></properties>
<settings>
<setting name="logImpl" value="STDOUT_LOGGING"/>
<!-- 设置映射属性名对应 user_name 对应 userName-->
<setting name="mapUnderscoreToCamelCase" value="true"/>
</settings>
<!--配置类型别名-->
<typeAliases>
<package name="com.springbatis.bean"/>
</typeAliases>
</configuration>
spring.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
https://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<!-- spring开启注解扫描 -->
<context:component-scan base-package="com.springbatis"></context:component-scan>
<import resource="mybatis.xml"></import>
</beans>
以上是关于创建一个Spring集成MyBatis的项目的主要内容,如果未能解决你的问题,请参考以下文章
Spring+SpringMvc+Mybatis框架集成搭建教程一(背景介绍及项目创建)