先看mybatis的配置文件
<?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>
<!--这里对应的是数据表的javabean文件-->
<typeAlias alias="role" type="com.plat.role.domain.Role"/>
</typeAliases>
</configuration>
再看spring的配置文件
<?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"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd">
<context:annotation-config/>
<!-- 配置自动扫描的包,这个如果spring是注解的方式注入bean就用到 -->
<context:component-scan base-package="com.plat"></context:component-scan>
<!-- 配置数据源 -->
<!-- 导入资源文件 -->
<context:property-placeholder location="classpath:/config/database.properties"/>
<!-- c3p0 数据源 -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="user" value="${jdbc.username}"></property>
<property name="password" value="${jdbc.password}"></property>
<property name="driverClass" value="${jdbc.driver}"></property>
<property name="jdbcUrl" value="${jdbc.url}"></property>
<property name="initialPoolSize" value="5"></property>
<property name="minPoolSize" value="5"/>
<property name="maxPoolSize" value="10"/>
<property name="maxIdleTime" value="600"/>
<property name="maxStatements" value="0"/>
</bean>
<!-- mapper配置 -->
<!-- 让spring管理sqlsessionfactory 使用mybatis和spring整合包中的 -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!-- 数据库连接池 -->
<property name="dataSource" ref="dataSource" />
<!-- 加载mybatis的全局配置文件 -->
<property name="configLocation" value="classpath:mybatis/mybatis-config.xml" />
<!-- 加载映射文件 -->
<property name="mapperLocations" value="classpath:com/plat/*/mapper/*.xml" />
</bean>
<!-- 配置扫描器 扫描 所有映射接口类-->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<!-- 扫描me.gacl.dao这个包以及它的子包下的所有映射接口类 -->
<property name="basePackage" value="com.plat.*.mapper" />
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" />
</bean>
<!--=====================配置事务======================-->
<!-- 配置 Spring 的声明式事务 -->
<!-- 1. 配置事务管理器 -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean>
<!-- 2. 配置事务属性, 需要事务管理器 -->
<tx:advice id="txAdvice" transaction-manager="transactionManager" >
<tx:attributes >
<!-- propagation :事务传播行为
isolation :事务隔离级别
-->
<!-- REQUIRED 这种事务传播行为是默认的,当一个事务方法运行调用另一个事务方法时
被调的那个方法将加入到调用方法的事务中
REQUIRES_NEW 注意,这种事务传播行为, 会新起一个事务,而原调它的方法的事务会挂起
容易发生违反数据原子性的问题
no-rollback-for 指定哪些异常不会滚
rollback-for 将被触发进行回滚的 Exception(s);以逗号分开。
read-only 该事务是否只读?默认false
timeout 指定事务运行时间,以秒为单位,如果指定时间之前没执行完,强制回滚 默认-1
-->
<tx:method name="get*" read-only="true" propagation="REQUIRED" no-rollback-for="" />
<tx:method name="find*" read-only="true" propagation="REQUIRED" rollback-for="" />
<tx:method name="add*" propagation="REQUIRED" isolation="DEFAULT"/>
<tx:method name="delete*" propagation="REQUIRED"/>
<tx:method name="update*" propagation="REQUIRED"/>
<!-- rollback-for : 异常导致回滚 -->
<tx:method name="*" rollback-for="java.lang.Throwable" timeout="10"/>
</tx:attributes >
</tx:advice>
<!-- 3. 配置事务切点, 并把切点和事务属性关联起来 -->
<aop:config>
<!-- execution(* com.luowg.*.service.*.*(..)) 匹配包下,任意返回值,任意方法名,任意方法参数 -->
<aop:pointcut expression="execution(* com.plat.*.service.impl.*(..))"
id="txPointcut"/>
<aop:advisor advice-ref="txAdvice" pointcut-ref="txPointcut"/>
</aop:config>
<bean class="com.plat.commons.util.ApplicationContextHolder" lazy-init="false"/>
</beans>
再看我的资源文件
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc\:mysql\://localhost\:3306/ordro?useUnicode\=true&characterEncoding\=utf-8&autoReconnect\=true&failOverReadOnly\=false
jdbc.username=root
jdbc.password=123456
jdbc.initPoolSize=5
jdbc.maxPoolSize=10
再看看我的role类
public class Role {
/**
* 角色ID
*/
private Long roleID;
/**
* 角色名称
*/
private String roleName;
/**
* 角色说明
*/
private String roleExplain;
/**
* 维护时间
*/
private String roleMaintainTime;
/**
* 维护者
*/
private String roleMaintainUser;
/**
* 多个id
*@2016年6月13日
*@return
*/
private Long[] ids;
public Long[] getIds() {
return ids;
}
public void setIds(Long[] ids) {
this.ids = ids;
}
public Long getRoleID() {
return roleID;
}
public void setRoleID(Long roleID) {
this.roleID = roleID;
}
public String getRoleName() {
return roleName;
}
public void setRoleName(String roleName) {
this.roleName = roleName;
}
public String getRoleExplain() {
return roleExplain;
}
public void setRoleExplain(String roleExplain) {
this.roleExplain = roleExplain;
}
public String getRoleMaintainTime() {
return roleMaintainTime;
}
public void setRoleMaintainTime(String roleMaintainTime) {
this.roleMaintainTime = roleMaintainTime;
}
public String getRoleMaintainUser() {
return roleMaintainUser;
}
public void setRoleMaintainUser(String roleMaintainUser) {
this.roleMaintainUser = roleMaintainUser;
}
}
再看看我的数据表
然后最后看我的映射文件
写映射文件时先写映射接口
import java.util.List;
import java.util.Map;
import com.plat.role.domain.Role;
public interface RoleMapper {
public List<Role> getById(Long resId);
/**
* 获取所有资源
*/
public List<Role> findAll();
public void addRole(Map role);
public void deleteByIds(Object[] ids);
/**
* 分页获取资源
* @param param
* @return
*/
public List<Role> findPageByParam(Map param) ;
/**
* 与分页配合,获取资源总数
* @param param
* @return
*/
public int getCountByParam(Map param);
public void updateById(Map role);
public void deleteById(Long id);
}
然后我们的xml映射文件时实现这个接口的
<mapper namespace="com.plat.role.mapper.RoleMapper">
这句写接口的绝对路径
<?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.plat.role.mapper.RoleMapper">
<resultMap id="roleResultMap" type="role">
<id column="sys_role_id" property="roleID" />
<result column="sys_role_name" property="roleName"/>
<result column="sys_role_explain" property="roleExplain" />
<result column="sys_role_maintain_time" property="roleMaintainTime" />
<result column="sys_role_maintain_user" property="roleMaintainUser" />
</resultMap>
<select id="getById" parameterType="Long" resultMap="roleResultMap"></select>
<select id="findAll" resultMap="roleResultMap"></select>
<insert id="addRole" parameterType="java.util.Map"></insert>
<update id="updateById" parameterType="java.util.Map"></update>
<!-- 分页查询 -->
<select id="findPageByParam" parameterType="java.util.Map" resultMap="roleResultMap">
</select>
<!-- 查询总条数 ,结合分页使用-->
<select id="getCountByParam" parameterType="java.util.Map" resultType="INTEGER">
</select>
<!-- 分页查询结束 -->
<delete id="deleteByIds" parameterType="Object[]"> </delete>
<delete id="deleteById" parameterType="Long"></delete>
</mapper>