MyBatis和SpringMVC整合
jar准备=
Dao方式整合
<?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:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.3.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.3.xsd">
<!-- jdbc配置文件 -->
<context:property-placeholder location="classpath:jdbc.properties"/>
<!-- 数据库 -->
<bean id="dataSource" class="org.apache.commons.dbcp2.BasicDataSource">
<property name="driverClassName" value="${jdbc.driver}"/>
<property name="url" value="${jdbc.url}" />
<property name="username" value="${jdbc.user}"/>
<property name="password" value="${jdbc.password}"/>
</bean>
<!-- 事务管理器 -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean>
<!-- 注册事务管理器驱动 -->
<tx:annotation-driven transaction-manager="transactionManager"/>
<!-- mybatis 关联 -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!-- 注入数据源 -->
<property name="dataSource" ref="dataSource"></property>
<!-- 指定核心配置文件 -->
<property name="configLocation" value="classpath:mybatis-config.xml"></property>
</bean>
<!-- bean -->
<bean id="customerDao" class="com.ruanyuan.dao.impl.CustomerDaoImpl">
<property name="sqlSessionFactory" ref="sqlSessionFactory"></property>
</bean>
</beans>
<?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>
<package name="com.ruanyuan.pojo"/>
</typeAliases>
<!-- 配置映射文件 -->
<mappers>
<mapper resource="com/ruanyuan/pojo/CustomerMapper.xml"/>
</mappers>
</configuration>
Mapper接口整合
applicationContext.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:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.3.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.3.xsd">
<!-- jdbc配置文件 -->
<context:property-placeholder location="classpath:jdbc.properties"/>
<!-- 数据库 -->
<bean id="dataSource" class="org.apache.commons.dbcp2.BasicDataSource">
<property name="driverClassName" value="${jdbc.driver}"/>
<property name="url" value="${jdbc.url}" />
<property name="username" value="${jdbc.user}"/>
<property name="password" value="${jdbc.password}"/>
</bean>
<!-- 事务管理器 -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean>
<!-- 注册事务管理器驱动 -->
<tx:annotation-driven transaction-manager="transactionManager"/>
<!-- mybatis 关联 -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!-- 注入数据源 -->
<property name="dataSource" ref="dataSource"></property>
<!-- 指定核心配"src/applicationContext.xml"置文件 -->
<property name="configLocation" value="classpath:mybatis-config.xml"></property>
</bean>
<!-- mapper代理开发 扫描包 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.ruanyuan.mapper"></property>
</bean>
</beans>
jdbc.properties
jdbc.url=jdbc:mysql://localhost:3306/systop_crm?useUnicode=true&characterEncoding=UTF8&useSSL=true
jdbc.user=root
jdbc.password=
log4j.properties
# Global logging configuration
log4j.rootLogger=ERROR, stdout
# MyBatis logging configuration...
log4j.logger.com.ruanyuan=DEBUG
# Console output...
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
yout.ConversionPattern=%5p [%t] - %m%n
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>
<package name="com.ruanyuan.pojo"/>
</typeAliases>
</configuration>
mapper接口和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.ruanyuan.mapper.BaseDictMapper">
<insert id="addBaseDict" parameterType="baseDict">
insert into base_dict(dict_id,dict_type_code,dict_type_name,dict_enable,dict_memo)
values(#{dict_id},#{dict_type_code},#{dict_type_name},#{dict_enable},#{dict_memo})
</insert>
</mapper>
text测试
public class Test1 {
@Test
public void addBaseDict() {
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
BaseDictMapper baseDictMapper =(BaseDictMapper) applicationContext.getBean(BaseDictMapper.class);
System.out.println("================================");
BaseDict baseDict1 = new BaseDict(null,"1002","月营业额","1","");
int i = baseDictMapper.addBaseDict(baseDict1);
if(i >0) {
System.out.println("添加成功");
}
}
}
Mapper接口整合使用事务
seDict baseDict1 = new BaseDict(null,“1002”,“月营业额”,“1”,"");
int i = baseDictMapper.addBaseDict(baseDict1);
if(i >0) {
System.out.println("添加成功");
}
}
}
## Mapper接口整合使用事务