实体beanpublic class User { Integer age; //年龄 Integer gender; //性别 Integer id; String mail; //邮箱 String nickname; String password; String site; //个人站点 String username; public User() { super(); // TODO Auto-generated constructor stub }//忽略无参构造和get,set代码}实体类的UserDaopublic interface UserDao { void save(User user); List<User> findAll(); User findById(Integer id); void delete(Integer id); void update(User user); List<User> findByPage(UserPage page); Integer getCount(UserPage page);}实体类UserDao的实现UserDaoImpl-----public class UserDaoImpl implements UserDao{ //在此处注入一个UserMapper private UserMapper userMapper; public void setUserMapper(UserMapper userMapper) { this.userMapper = userMapper; } @Override public void delete(Integer id) { userMapper.delete(id); } @Override public List<User> findAll() { return userMapper.findAll(); } @Override public User findById(Integer id) { return userMapper.findById(id); } @Override public void save(User user) { userMapper.save(user);// throw new RuntimeException("Error"); } @Override public void update(User user) { userMapper.update(user); } @Override public List<User> findByPage(UserPage page) { return userMapper.findByPage(page); } @Override public Integer getCount(UserPage page) { return userMapper.getCount(page); }}我想大家现在都在关心UserDaoImpl中的UserMapper是从哪里来的:首先我们需要一个分页查询的中间类UserPage:public class UserPage { Integer firstRec; Integer pageSize; User user; }其次就是UserMapper映射了~~~~~~~~package com.pb.mybatis.mapper;public interface UserMapper { void save(User user); List<User> findAll(); User findById(Integer id); void delete(Integer id); void update(User user); List<User> findByPage(UserPage page); Integer getCount(UserPage page);}注意UserMapper要与在同一个包下的UserMapper.xml同名,且在同一包下:以下就是UserMapper的映射文件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"> <!-- Mapper文件最好放在与Dao接口同一目次下 --><mapper namespace="com.pb.mybatis.mapper.UserMapper"> <!-- 定义数据库字段与实体对象的映射关系 --> <resultMap type="com.pb.mybatis.entity.User" id="resultUser"> <id property="id" column="ID"/> <result property="age" column="AGE"/> <result property="gender" column="GENDER"/> <result property="mail" column="MAIL"/> <result property="nickname" column="NICKNAME"/> <result property="password" column="PASSWORD"/> <result property="site" column="SITE"/> <result property="username" column="USERNAME"/> </resultMap> <parameterMap type="com.pb.mybatis.entity.User" id="paramUser"> <parameter property="id"/> <parameter property="age"/> <parameter property="gender"/> <parameter property="mail"/> <parameter property="nickname"/> <parameter property="password"/> <parameter property="site"/> <parameter property="username"/> </parameterMap> <!-- 定义要操纵的SQL语句 --> <insert id="save" parameterType="com.pb.mybatis.entity.User"> INSERT INTO user(id,age,gender,username,password,mail,nickname,site) VALUES(#{id},#{age},#{gender},#{username},#{password},#{mail},#{nickname},#{site}) </insert> <select id="findAll" resultMap="resultUser"> SELECT * FROM user </select> <select id="findById" parameterType="Integer" resultMap="resultUser"> SELECT * FROM user WHERE id=#{value} </select> <delete id="delete" parameterType="Integer"> DELETE FROM user WHERE id=#{value} </delete> <update id="update" parameterType="com.pb.mybatis.entity.User" > UPDATE user <set> <if test="age != null">age=#{age},</if> <if test="gender != null">gender=#{gender},</if> <if test="username != null">username=#{username},</if> <if test="password != null">password=#{password},</if> <if test="mail != null">mail=#{mail},</if> <if test="nickname != null">nickname=#{nickname},</if> <if test="site != null">site=#{site},</if> </set> WHERE id=#{id} </update> <select id="findByPage" parameterType="com.pb.mybatis.util.UserPage" resultMap="resultUser"> SELECT * FROM user WHERE 1=1 <if test="user != null"> <if test="user.nickname != null">and nickname like #{user.nickname}</if> <if test="user.username != null">and username like #{user.username}</if> </if> LIMIT #{firstRec},#{pageSize} </select> <select id="getCount" parameterType="com.pb.mybatis.util.UserPage" resultType="Integer"> SELECT count(*) FROM user WHERE 1=1 <if test="user != null"> <if test="user.nickname != null">and nickname like #{user.nickname}</if> <if test="user.username != null">and username like #{user.username}</if> </if> </select></mapper>现在是UserService业务层:package com.pb.mybatis.service;import java.util.List;import com.pb.mybatis.entity.User;import com.pb.mybatis.util.UserPage;public interface UserService { void saveUser(User user); void updateUser(User user); void deleteUser(Integer id); User findById(Integer id); List<User> findAll(); List<User> findByPage(UserPage page); Integer getCount(UserPage page);}UserServiceImpl实现:public class UserServiceImpl implements UserService{ private UserDao userDao; public void setUserDao(UserDao userDao) { this.userDao = userDao; } @Override public void saveUser(User user) { userDao.save(user); } @Override public void deleteUser(Integer id) { userDao.delete(id); } @Override public void updateUser(User user) { userDao.update(user); } @Override public List<User> findAll() { return userDao.findAll(); } @Override public User findById(Integer id) { return userDao.findById(id); } @Override public List<User> findByPage(UserPage page) { return userDao.findByPage(page); } @Override public Integer getCount(UserPage page) { return userDao.getCount(page); }}注意,以下就是spring的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: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-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/tx[/url] http://www.springframework.org/schema/tx/spring-tx-3.0.xsd http://www.springframework.org/schema/aop [url]http://www.springframework.org/schema/aop/spring-aop-3.0.xsd"> <!-- 将所有的配置文件没有放在外面:简单,但可维护性不高 --> <context:annotation-config/> <!-- 扫描物理路径及注册 --> <context:component-scan base-package="com.pb.mybatis.*"/> <!-- jdbc.properties Directory --> <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="locations" value="classpath:jdbc.properties"/> </bean> <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <property name="driverClassName" value="${jdbc.driverClassName}"/> <property name="url" value="${jdbc.url}"/> <property name="username" value="${jdbc.username}"/> <property name="password" value="${jdbc.password}"/> </bean> <!-- SqlSessionFactory --> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource"/> </bean> <!-- ScanMapperFiles --> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value="com.pb.mybatis.mapper"/> </bean> <!-- TransactionManager Contorl--> <bean name="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource"/> </bean> <tx:advice id="userTxAdvice" transaction-manager="transactionManager"> <tx:attributes> <tx:method name="save*" propagation="REQUIRED" read-only="false" rollback-for="java.lang.Exception" no-rollback-for="java.lang.RuntimeException"/> <tx:method name="delete*" propagation="REQUIRED" read-only="false" rollback-for="java.lang.Exception" no-rollback-for="java.lang.RuntimeException"/> <tx:method name="update*" propagation="REQUIRED" read-only="false" rollback-for="java.lang.Exception" no-rollback-for="java.lang.RuntimeException"/> <tx:method name="find*" propagation="SUPPORTS" read-only="true"/> </tx:attributes> </tx:advice> <aop:config> <!--把事务控制在Service层--> <aop:pointcut id="pc" expression="execution(* com.pb.mybatis.service.*.*(..))"/> <aop:advisor advice-ref="userTxAdvice" pointcut-ref="pc"/> </aop:config> <!-- Dao Bean--> <bean id="userDao" class="com.pb.mybatis.dao.impl.UserDaoImpl" autowire="byName"/> <!-- Service Bean --> <bean id="userService" class="com.pb.mybatis.service.impl.UserServiceImpl" autowire="byName"/></beans>
|
|