在idea上的效果:
具体文件组成:
applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
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
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd">
<!--组件注解解析器-->
<context:annotation-config></context:annotation-config>
<!--扫描包-->
<context:component-scan base-package="crm"></context:component-scan>
<!--AOP自动代理-->
<aop:aspectj-autoproxy></aop:aspectj-autoproxy>
<!--引入属性配置文件-->
<!-- 1.导入外部文件db.properties -->
<context:property-placeholder location="classpath:db.properties"></context:property-placeholder>
<!-- 2.配置数据源 -->
<bean id="datasource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close">
<property name="driverClassName" value="${db.driverClassName}"></property>
<property name="url" value="${db.url}"></property>
<property name="username" value="${db.username}"></property>
<property name="password" value="${db.password}"></property>
<property name="maxActive" value="${db.maxActive}"></property>
</bean>
<!-- 3.配置sessionFactory -->
<bean id="sessionfactorybean" class="org.mybatis.spring.SqlSessionFactoryBean">
<!--配置数据-->
<property name="dataSource" ref="datasource"></property>
<!--配置mybatis-config.xml相关文件-->
<property name="configLocation" value="classpath:mybatis-config.xml"></property>
<!--配置mapper对应的映射路径-->
<property name="mapperLocations" value="classpath:crm/mapper/usermapper/*mapper.xml"></property>
<!--配置别名所在类的包名-->
<property name="typeAliasesPackage" value="crm.domain"></property>
</bean>
<!-- 4.配置事务管理器 -->
<!--what:事务管理器的配置-->
<bean id="txmanager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="datasource"></property>
</bean>
<!-- 5.配置事务模板 -->
<!--when:事务增强的时机-->
<tx:advice id="txadvice" transaction-manager="txmanager">
<tx:attributes>
<tx:method name="list*" read-only="true"/>
<tx:method name="query*" read-only="true"/>
<tx:method name="get*" read-only="true"/>
<tx:method name="*"/>
</tx:attributes>
</tx:advice>
<!-- 6.AOP切面配置 -->
<!--在哪些包下的哪些类进行增强-->
<aop:config>
<!--切入点-->
<aop:pointcut id="txpoint" expression="execution(* crm.service.*.*(..))"></aop:pointcut>
<!--关联-->
<aop:advisor advice-ref="txadvice" pointcut-ref="txpoint"></aop:advisor>
</aop:config>
<!--7.定义service,Mapper对应bean -->
<bean id="mapper" class="org.mybatis.spring.mapper.MapperFactoryBean">
<property name="mapperInterface" value="crm.mapper.usermapper.usermapper"></property>
</bean>
<bean id="service" class="crm.service.impl.Iuserserviceimpl">
<property name="usermapper" ref="mapper"></property>
</bean>
</beans>
db.properties
db.driverClassName=com.mysql.jdbc.Driver
db.url=jdbc:mysql://localhost:3306/mybatis
db.username=root
db.password=123456
db.maxActive=5
log4j.properties
# Global logging configuration
log4j.rootLogger=ERROR, stdout
# MyBatis logging configuration...
log4j.logger.crm.mapper.usermapper=TRACE //为具体操作数据库的那个xml所在的路径
# Console output...
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.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>
<!--引入db.properties-->
<properties resource="db.properties"></properties>
<settings>
<!--启用延迟加载-->
<setting name="lazyLoadingEnabled" value="true"/>
<!--禁用积极延迟加载-->
<setting name="aggressiveLazyLoading" value="false"/>
<!--延迟加载触发的方法-->
<setting name="lazyLoadTriggerMethods" value="clone"/>
</settings>
</configuration>
user类
package crm.domain;
import org.apache.ibatis.type.Alias;
//配置别名
@Alias("user")
public class user {
private Long id;
private String username;
private String password;
private Integer age;
public user(Long id, String username, String password, Integer age) {
this.id = id;
this.username = username;
this.password = password;
this.age = age;
}
public user(String username, String password, Integer age) {
this.username = username;
this.password = password;
this.age = age;
}
public user() {
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
@Override
public String toString() {
return "user{" +
"id=" + id +
", username='" + username + '\'' +
", password='" + password + '\'' +
", age=" + age +
'}';
}
}
usermapper接口
package crm.mapper.usermapper;
import crm.domain.user;
public interface usermapper {
void save(user u);
}
usermapper.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">
<!--namespace:用来存放对应的接口名字-->
<mapper namespace="crm.mapper.usermapper.usermapper">
<!--
id:usermapper接口中对应的名字
parameterType:传入的参数
keyColumn:数据库中应该自增长的字段名
keyProperty:自增长的字段名应该注入到实体类的字段名
useGeneratedKeys:标记这个标签需要数据库自增长的id
-->
<insert id="save" parameterType="crm.domain.user" keyColumn="id" keyProperty="id" useGeneratedKeys="true">
INSERT INTO user (username,password,age)VALUES (#{username},#{password},#{age})
</insert>
</mapper>
Iuserserviceimpl实现类
package crm.service.impl;
import crm.domain.user;
import crm.mapper.usermapper.usermapper;
import crm.service.Iuserservice;
public class Iuserserviceimpl implements Iuserservice {
private usermapper usermapper;
public crm.mapper.usermapper.usermapper getUsermapper() {
return usermapper;
}
public void setUsermapper(crm.mapper.usermapper.usermapper usermapper) {
this.usermapper = usermapper;
}
@Override
public void save(user u) {
usermapper.save(u);
//int a=10/0;//(用来检测事务配置是否成功);
}
}
Iuserservice接口
package crm.service;
import crm.domain.user;
public interface Iuserservice {
void save(user u);
}
Iuserservicetest测试类
package test.Iuserserviceimpltest;
import crm.domain.user;
import crm.service.Iuserservice;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext.xml")
public class Iuserservicetest {
@Autowired
Iuserservice service;
@Test
public void testsave(){
service.save(new user("洲洲","123456",20));
}
}
测试结果如下: