这是本文档旧的修订版!
框架 | 版本 |
---|---|
springmvc | 4.0.5.RELEASE |
mybatis | 3.2.7 |
spring | 4.0.5.RELEASE |
freemarker | 2.3.20 |
maven | 3.0 |
package com.gxx.record.base.dao; import org.springframework.stereotype.Repository; import com.gxx.record.base.vo.User; @Repository public interface UserMapper { int deleteByPrimaryKey(Integer id); int insert(User record); int insertSelective(User record); User selectByPrimaryKey(Integer id); int updateByPrimaryKeySelective(User record); int updateByPrimaryKey(User record); /** * 根据姓名查用户 * @param name * @return */ User getUserByName(String name); }
<?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.gxx.record.base.dao.UserMapper" > <resultMap id="BaseResultMap" type="com.gxx.record.base.vo.User" > <id column="id" property="id" jdbcType="INTEGER" /> <result column="name" property="name" jdbcType="VARCHAR" /> <result column="password" property="password" jdbcType="VARCHAR" /> <result column="create_date" property="createDate" jdbcType="VARCHAR" /> <result column="create_time" property="createTime" jdbcType="VARCHAR" /> </resultMap> <sql id="Base_Column_List" > id, name, password, create_date, create_time </sql> <select id="selectByPrimaryKey" resultMap="BaseResultMap" parameterType="java.lang.Integer" > select <include refid="Base_Column_List" /> from user where id = #{id,jdbcType=INTEGER} </select> <delete id="deleteByPrimaryKey" parameterType="java.lang.Integer" > delete from user where id = #{id,jdbcType=INTEGER} </delete> <insert id="insert" parameterType="com.gxx.record.base.vo.User" > insert into user (id, name, password, create_date, create_time) values (#{id,jdbcType=INTEGER}, #{name,jdbcType=VARCHAR}, #{password,jdbcType=VARCHAR}, #{createDate,jdbcType=VARCHAR}, #{createTime,jdbcType=VARCHAR}) </insert> <insert id="insertSelective" parameterType="com.gxx.record.base.vo.User" > insert into user <trim prefix="(" suffix=")" suffixOverrides="," > <if test="id != null" > id, </if> <if test="name != null" > name, </if> <if test="password != null" > password, </if> <if test="createDate != null" > create_date, </if> <if test="createTime != null" > create_time, </if> </trim> <trim prefix="values (" suffix=")" suffixOverrides="," > <if test="id != null" > #{id,jdbcType=INTEGER}, </if> <if test="name != null" > #{name,jdbcType=VARCHAR}, </if> <if test="password != null" > #{password,jdbcType=VARCHAR}, </if> <if test="createDate != null" > #{createDate,jdbcType=VARCHAR}, </if> <if test="createTime != null" > #{createTime,jdbcType=VARCHAR}, </if> </trim> </insert> <update id="updateByPrimaryKeySelective" parameterType="com.gxx.record.base.vo.User" > update user <set > <if test="name != null" > name = #{name,jdbcType=VARCHAR}, </if> <if test="password != null" > password = #{password,jdbcType=VARCHAR}, </if> <if test="createDate != null" > create_date = #{createDate,jdbcType=VARCHAR}, </if> <if test="createTime != null" > create_time = #{createTime,jdbcType=VARCHAR}, </if> </set> where id = #{id,jdbcType=INTEGER} </update> <update id="updateByPrimaryKey" parameterType="com.gxx.record.base.vo.User" > update user set name = #{name,jdbcType=VARCHAR}, password = #{password,jdbcType=VARCHAR}, create_date = #{createDate,jdbcType=VARCHAR}, create_time = #{createTime,jdbcType=VARCHAR} where id = #{id,jdbcType=INTEGER} </update> <select id="getUserByName" resultMap="BaseResultMap" parameterType="java.lang.String" > select <include refid="Base_Column_List" /> from user where name = #{name,jdbcType=VARCHAR} </select> </mapper>
package com.gxx.record.base.vo; public class User { private Integer id; private String name; private String password; private String createDate; private String createTime; public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name == null ? null : name.trim(); } public String getPassword() { return password; } public void setPassword(String password) { this.password = password == null ? null : password.trim(); } public String getCreateDate() { return createDate; } public void setCreateDate(String createDate) { this.createDate = createDate == null ? null : createDate.trim(); } public String getCreateTime() { return createTime; } public void setCreateTime(String createTime) { this.createTime = createTime == null ? null : createTime.trim(); } }
package com.gxx.record.dto; /** * <dl> * <dt><b>Title:</b></dt> * <dd> * 基础传输对象 * </dd> * <dt><b>Description:</b></dt> * <dd> * <p>none * </dd> * </dl> * * @author Administrator * @version 1.0, 2015年6月18日 * @since record * */ public class BaseDto { String message;//信息 boolean success;//是否成功 public String getMessage() { return message; } public void setMessage(String message) { this.message = message; } public boolean getSuccess() { return success; } public void setSuccess(boolean success) { this.success = success; } }
package com.gxx.record.dto; /** * <dl> * <dt><b>Title:</b></dt> * <dd> * 用户传输对象 * </dd> * <dt><b>Description:</b></dt> * <dd> * <p>none * </dd> * </dl> * * @author Administrator * @version 1.0, 2015年6月18日 * @since record * */ public class UserDto extends BaseDto { private Integer id;//id private String name;//姓名 private String password;//密码 private String createDate;//创建日期 private String createTime;//创建时间 public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public String getCreateDate() { return createDate; } public void setCreateDate(String createDate) { this.createDate = createDate; } public String getCreateTime() { return createTime; } public void setCreateTime(String createTime) { this.createTime = createTime; } }
package com.gxx.record.service; import com.gxx.record.base.vo.User; /** * <dl> * <dt><b>Title:</b></dt> * <dd> * 用户服务接口 * </dd> * <dt><b>Description:</b></dt> * <dd> * <p>none * </dd> * </dl> * * @author Administrator * @version 1.0, 2015年6月18日 * @since record * */ public interface UserService { /** * 新增用户 * @param user */ public void doSaveUser(User user); /** * 根据姓名查用户 * @param name * @return */ public User getUserByName(String name); }
package com.gxx.record.service.impl; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import com.gxx.record.base.dao.UserMapper; import com.gxx.record.base.vo.User; import com.gxx.record.service.UserService; /** * <dl> * <dt><b>Title:</b></dt> * <dd> * 用户服务实现类 * </dd> * <dt><b>Description:</b></dt> * <dd> * <p>none * </dd> * </dl> * * @author Administrator * @version 1.0, 2015年6月18日 * @since record * */ @Service("userService") public class UserServiceImpl implements UserService { @Autowired private UserMapper userDao; /** * 新增用户 * @param user */ public void doSaveUser(User user) { userDao.insert(user); } /** * 根据姓名查用户 * @param name * @return */ public User getUserByName(String name) { return userDao.getUserByName(name); } }
package com.gxx.record.web.user; import javax.servlet.http.HttpServletRequest; import org.apache.log4j.Logger; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.ResponseBody; import com.gxx.record.base.vo.User; import com.gxx.record.dto.UserDto; import com.gxx.record.service.UserService; /** * QueryAllocateController负责查询调拨 * * @author gxx */ @Controller @RequestMapping("/user/") public class UserController { /** * 日志处理器 */ private final Logger logger = Logger.getLogger(UserController.class); @Autowired private UserService userService; @RequestMapping(value = "/preRegistFtl", method = RequestMethod.GET) public String preRegistFtl() { return "user/preRegistFtl"; } @RequestMapping(value = "/preRegistJsp", method = RequestMethod.GET) public String preRegistJsp() { return "user/preRegistJsp"; } /** * 注册 * @param request * @param userDto * @return */ @RequestMapping(value = "/registJsp",produces="application/json") public @ResponseBody UserDto registJsp(HttpServletRequest request, UserDto userDto) { logger.info("用户注册:姓名[" + userDto.getName() + "],密码[" + userDto.getPassword() + "]"); /** * 1.判用户名是否存在 */ User user = userService.getUserByName(userDto.getName()); if(user != null){ userDto.setSuccess(Boolean.FALSE.booleanValue()); userDto.setMessage("用户名[" + userDto.getName() + "]已存在!"); return userDto; } /** * 2.创建用户对象 并 新增用户 */ user = new User(); user.setName(userDto.getName()); user.setPassword(userDto.getPassword()); user.setCreateDate("20150618"); user.setCreateTime("000000"); userService.doSaveUser(user); /** * 3.返回结果 */ userDto.setSuccess(Boolean.TRUE.booleanValue()); userDto.setMessage("注册成功!"); return userDto; } /** * 注册 * @param request * @param userDto * @return */ @RequestMapping(value = "/registFtl") public String registFtl(HttpServletRequest request, UserDto userDto) { logger.info("用户注册:姓名[" + userDto.getName() + "],密码[" + userDto.getPassword() + "]"); /** * 1.判用户名是否存在 */ User user = userService.getUserByName(userDto.getName()); if(user != null){ userDto.setSuccess(Boolean.FALSE.booleanValue()); userDto.setMessage("用户名[" + userDto.getName() + "]已存在!"); return "user/result"; } /** * 2.创建用户对象 并 新增用户 */ user = new User(); user.setName(userDto.getName()); user.setPassword(userDto.getPassword()); user.setCreateDate("20150618"); user.setCreateTime("000000"); userService.doSaveUser(user); /** * 3.返回结果 */ userDto.setSuccess(Boolean.TRUE.booleanValue()); userDto.setMessage("注册成功!"); return "user/result"; } }
<?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:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:jpa="http://www.springframework.org/schema/data/jpa" xmlns:security="http://www.springframework.org/schema/security" xsi:schemaLocation=" http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security-3.2.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.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"> <!-- Activates annotation-based bean configuration --> <context:annotation-config /> <!-- Scans for application @Components to deploy --> <context:component-scan base-package="com.gxx.record" /> <!-- 数据库配置文件位置 --> <context:property-placeholder location="classpath:/jdbc.properties" /> <!-- 配置dbcp数据源 --> <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> <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> <!-- 使用JDBC事务 --> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource" /> </bean> <!-- AOP配置事物 --> <tx:advice id="transactionAdvice" transaction-manager="transactionManager"> <tx:attributes> <tx:method name="query*" read-only="true" propagation="REQUIRED" /> <tx:method name="delete*" propagation="REQUIRED" /> <tx:method name="update*" propagation="REQUIRED" /> <tx:method name="insert*" propagation="REQUIRED" /> <tx:method name="*" propagation="REQUIRED" /> </tx:attributes> </tx:advice> <!-- 配置AOP切面 --> <aop:config> <aop:pointcut id="transactionPointcut" expression="execution(* com.gxx.record.service.*.*(..))"/> <aop:advisor pointcut-ref="transactionPointcut" advice-ref="transactionAdvice"/> </aop:config> <!-- 使用annotation注解方式配置事务 --> <tx:annotation-driven transaction-manager="transactionManager" /> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource" /> <property name="configLocation" value="classpath:mybatis.xml"></property> <property name="mapperLocations" value="classpath:com/gxx/record/base/mapping/*.xml"></property> </bean> <!-- 配置SQLSession模板 --> <bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate"> <constructor-arg index="0" ref="sqlSessionFactory" /> </bean> <!--扫描basePackage下所有以@Repository注解的接口 --> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" /> <property name="basePackage" value="com.gxx.record"/> <property name="annotationClass" value="org.springframework.stereotype.Repository"/> </bean> </beans>