(1)User实体类 (2)UserMappser.xml User类对应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.domain.UserMapper"> <!-- 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>UserDao层接口,定义crud接口方法
dao接口实现类,继承SqlSessionDaoSupport,利用注入的SqlSession实现功能
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获取时 不要使用username 会有问题 --> <property name="username" value="${user}"></property> <property name="password" value="${password}"></property> </bean> <!-- 配置c3p0连接池 --> <!-- <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"> <property name="driverClass" value="com.mysql.jdbc.Driver"></property> <property name="jdbcUrl" value="jdbc:mysql:///Wang?useUnicode=true&characterEncoding=UTF-8"></property> <property name="user" value="root"></property> <property name="password" value="root"></property> </bean> --> <!-- 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>User实体类对应的spring配置 需要在spring核心配置文件applicationConetex.xml中引入
user.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 http://www.springframework.org/schema/beans/spring-beans.xsd"> <!-- service --> <bean id="userService" class="cn.wang.service.UserServiceImpl"> <property name="userDao" ref="userDao"></property> </bean> <!-- dao --> <bean id="userDao" class="cn.wang.dao.UserDaoImpl"> <property name="sqlSessionFactory" ref="sqlSessionFactory"></property> </bean> </beans>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> <mappers> <!-- 映射注册 include --> <!-- resource 全路径 --> <mapper resource="cn/wang/domain/UserMapper.xml" /> </mappers> </configuration>数据库连接信息