UserDao层接口,定义crud接口方法
注解方式配置不需要实现dao接口,会自动实现并创建对象 只需要配置实体类对应的SQL映射
<?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="cn.wang.dao.IUserDao"> <!-- id标识 语句的唯一性 --> <!-- resultType 全类名 --> <select id="findAll" resultType="cn.wang.domain.User"> SELECT * FROM tbl_user </select> <!-- add添加 --> <!-- parameterType表示参数类型 全类名 --> <!-- 设置参数 用#{属性名称}表示值的获取 --> <insert id="add" parameterType="cn.wang.domain.User"> insert into tbl_user (uname,upsw,uage) values(#{uname},#{upsw},#{uage}) </insert> <delete id="deleteById" parameterType="java.io.Serializable"> delete from tbl_user where id=#{id} </delete> <update id="update" parameterType="cn.wang.domain.User"> update tbl_user set uname=#{uname},upsw=#{upsw},uage=#{uage} where id=#{id} </update> <select id="findById" resultType="cn.wang.domain.User" parameterType="java.io.Serializable"> select * from tbl_user where id=#{id} </select> </mapper>service层接口 一般在service层添加事务 (2)UserServiceImpl service层接口实现类,配置自动注入的属性
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:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.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-3.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd"> <!-- 引入数据库信息配置文件 --> <context:property-placeholder location="classpath:JDBC.properties"/> <!-- dataSource --> <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <property name="driverClassName" value="${driverClassName}"></property> <property name="url" value="${url}"></property> <!-- username获取时 不要使用user 会有问题 --> <property name="username" value="${username}"></property> <property name="password" value="${password}"></property> </bean> <!-- 配置bean对象 注解 基包扫描 --> <context:component-scan base-package="cn.wang"></context:component-scan> <!-- spring-mybatis整合注入mapper映射文件 --> <bean id="mapperScannerConfigurer" class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <!-- 指定要扫描的包 --> <property name="basePackage" value="cn.wang.dao"></property> <!-- sqlSessionFactory ,使用sqlSessionFactoryBeanName属性注入--> <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property> </bean> <!-- sessionFactory --> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <!-- dataSource --> <property name="dataSource" ref="dataSource"></property> </bean> <!-- tx --> <!-- 开启事务注解扫描 --> <tx:annotation-driven transaction-manager="transactionManager"/> <!-- 事务管理器 --> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <!-- dataSource --> <property name="dataSource" ref="dataSource"></property> </bean> </beans> --> <!-- sessionFactory --> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <!-- dataSource --> <property name="dataSource" ref="dataSource"></property> <!-- configLocation --> <property name="configLocation" value="classpath:conf.xml"></property> </bean> <!-- tx --> <!-- 事务管理的通知 :哪个方法需要使用事务通知 --> <tx:advice id="txAdvicd" transaction-manager="transactionManager"> <tx:attributes> <tx:method name="add"/> <tx:method name="delete*"/> <tx:method name="update*"/> </tx:attributes> </tx:advice> <!-- aop事务管理 --> <aop:config> <aop:advisor advice-ref="txAdvicd" pointcut="execution(* cn.wang.service.UserServiceImpl.*(..))"/> </aop:config> <!-- 事务管理器 --> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <!-- dataSource --> <property name="dataSource" ref="dataSource"></property> </bean> <!-- 导入外部文件 --> <import resource="classpath:user.xml" /> </beans>数据库连接信息
