一、POM.xml文件内容为:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.winphone</groupId>
<artifactId>TestMaven</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<name>TestMaven</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<springVersion>4.3.3.RELEASE</springVersion>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>${springVersion}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${springVersion}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>${springVersion}</version>
</dependency>
<!-- 数据库 -->
<dependency>
<groupId>commons-dbcp</groupId>
<artifactId>commons-dbcp</artifactId>
<version>1.2.2</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.8.4</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.0.4</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>1.0.0</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.29</version>
</dependency>
</dependencies>
<build>
<finalName>TestMaven</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>1.7</source> <!-- 源代码使用的开发版本 -->
<target>1.7</target> <!-- 需要生成的目标class文件的编译版本 -->
</configuration>
</plugin>
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<version>2.2</version>
<configuration>
<uriEncoding>UTF-8</uriEncoding>
<port>8088</port>
<path>/TestMaven</path> <!-- 应用的部署位置 -->
</configuration>
</plugin>
</plugins>
</build>
</project>
二、新建登录用户表
DROP TABLE IF EXISTS `op_login_user_`;
CREATE TABLE `op_login_user_` (
`login_user_id_` char(36) NOT NULL COMMENT '用户ID',
`user_name_` varchar(20) DEFAULT NULL COMMENT '用户名',
`user_type_` char(2) DEFAULT NULL COMMENT '用户类型',
`password_` char(36) DEFAULT NULL COMMENT '密码',
`online_status_` char(2) DEFAULT NULL COMMENT '用户在线状态',
`last_login_time_` varchar(20) DEFAULT NULL COMMENT '上次登录时间',
`pw_error_number_` char(2) DEFAULT NULL COMMENT '密码错误次数',
`login_user_status_` char(2) DEFAULT NULL COMMENT '登录用户状态',
PRIMARY KEY (`login_user_id_`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='110101登录用户表';
INSERT INTO `op_login_user_` VALUES ('f38b24b9-172a-4d2b-976f-f0cb42c4a1b0', 'admin', '01', 'e10adc3949ba59abbe56e057f20f883e', '00', null, '0', null);
三、新建文件LoginUserMapper.xml、mybatis-config.xml、ApplicationContext-service.xml
LoginUserMapper.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 namespace="com.winphone.mapper.LoginUserMapper" >
<resultMap id="BaseResultMap" type="com.winphone.entity.LoginUser" >
<id column="login_user_id_" property="loginUserId" jdbcType="CHAR" />
<result column="user_name_" property="userName" jdbcType="VARCHAR" />
<result column="user_type_" property="userType" jdbcType="CHAR" />
<result column="password_" property="password" jdbcType="CHAR" />
<result column="online_status_" property="onlineStatus" jdbcType="CHAR" />
<result column="login_user_status_" property="loginUserStatus" jdbcType="CHAR" />
<result column="last_login_time_" property="lastLoginTime" jdbcType="VARCHAR" />
<result column="pw_error_number_" property="pwErrorNumber" jdbcType="CHAR" />
</resultMap>
<sql id="Base_Column_List" >
login_user_id_, user_name_, user_type_, password_, online_status_,login_user_status_, last_login_time_, pw_error_number_
</sql>
<select id="selectByPrimaryKey" resultMap="BaseResultMap" parameterType="java.lang.String" >
select
<include refid="Base_Column_List" />
from op_login_user_
where login_user_id_ = #{loginUserId,jdbcType=CHAR}
</select>
<delete id="deleteByPrimaryKey" parameterType="java.lang.String" >
delete from op_login_user_
where login_user_id_ = #{loginUserId,jdbcType=CHAR}
</delete>
<insert id="insert" parameterType="com.winphone.entity.LoginUser" >
insert into op_login_user_ (login_user_id_, user_name_, user_type_,
password_, online_status_, login_user_status_, last_login_time_,
pw_error_number_)
values (#{loginUserId,jdbcType=CHAR}, #{userName,jdbcType=VARCHAR}, #{userType,jdbcType=CHAR},
#{password,jdbcType=CHAR}, #{onlineStatus,jdbcType=CHAR}, #{loginUserStatus,jdbcType=CHAR}, #{lastLoginTime,jdbcType=VARCHAR},
#{pwErrorNumber,jdbcType=CHAR})
</insert>
<insert id="insertSelective" parameterType="com.winphone.entity.LoginUser" >
insert into op_login_user_
<trim prefix="(" suffix=")" suffixOverrides="," >
<if test="loginUserId != null and loginUserId != ''" >
login_user_id_,
</if>
<if test="userName != null and userName != ''" >
user_name_,
</if>
<if test="userType != null and userType != ''" >
user_type_,
</if>
<if test="password != null and password != ''" >
password_,
</if>
<if test="onlineStatus != null and onlineStatus != ''" >
online_status_,
</if>
<if test="loginUserStatus != null and loginUserStatus != ''" >
login_user_status_,
</if>
<if test="lastLoginTime != null and lastLoginTime != ''" >
last_login_time_,
</if>
<if test="pwErrorNumber != null and pwErrorNumber != ''" >
pw_error_number_,
</if>
</trim>
<trim prefix="values (" suffix=")" suffixOverrides="," >
<if test="loginUserId != null and loginUserId != ''" >
#{loginUserId,jdbcType=CHAR},
</if>
<if test="userName != null and userName != ''" >
#{userName,jdbcType=VARCHAR},
</if>
<if test="userType != null and userType != ''" >
#{userType,jdbcType=CHAR},
</if>
<if test="password != null and password != ''" >
#{password,jdbcType=CHAR},
</if>
<if test="onlineStatus != null and onlineStatus != ''" >
#{onlineStatus,jdbcType=CHAR},
</if>
<if test="loginUserStatus != null and loginUserStatus != ''" >
#{loginUserStatus,jdbcType=CHAR},
</if>
<if test="lastLoginTime != null and lastLoginTime != ''" >
#{lastLoginTime,jdbcType=VARCHAR},
</if>
<if test="pwErrorNumber != null and pwErrorNumber != ''" >
#{pwErrorNumber,jdbcType=CHAR},
</if>
</trim>
</insert>
<update id="updateByPrimaryKeySelective" parameterType="com.winphone.entity.LoginUser" >
update op_login_user_
<set >
<if test="userName != null and userName != ''" >
user_name_ = #{userName,jdbcType=VARCHAR},
</if>
<if test="userType != null and userType != ''" >
user_type_ = #{userType,jdbcType=CHAR},
</if>
<if test="password != null and password != ''" >
password_ = #{password,jdbcType=CHAR},
</if>
<if test="onlineStatus != null and onlineStatus != ''" >
online_status_ = #{onlineStatus,jdbcType=CHAR},
</if>
<if test="loginUserStatus != null and loginUserStatus != ''" >
login_user_status_ = #{loginUserStatus,jdbcType=CHAR},
</if>
<if test="lastLoginTime != null and lastLoginTime != ''" >
last_login_time_ = #{lastLoginTime,jdbcType=VARCHAR},
</if>
<if test="pwErrorNumber != null and pwErrorNumber != ''" >
pw_error_number_ = #{pwErrorNumber,jdbcType=CHAR},
</if>
</set>
where login_user_id_ = #{loginUserId,jdbcType=CHAR}
</update>
<update id="updateByPrimaryKey" parameterType="com.winphone.entity.LoginUser" >
update op_login_user_
set user_name_ = #{userName,jdbcType=VARCHAR},
user_type_ = #{userType,jdbcType=CHAR},
password_ = #{password,jdbcType=CHAR},
online_status_ = #{onlineStatus,jdbcType=CHAR},
login_user_status_ = #{loginUserStatus,jdbcType=CHAR},
last_login_time_ = #{lastLoginTime,jdbcType=VARCHAR},
pw_error_number_ = #{pwErrorNumber,jdbcType=CHAR}
where login_user_id_ = #{loginUserId,jdbcType=CHAR}
</update>
<select id="listLoginUsers" parameterType="LoginUser" resultMap="BaseResultMap">
select * from op_login_user_
where 1=1
<if test="userName != null and userName != ''" >
and user_name_ = #{userName,jdbcType=CHAR}
</if>
<if test="userType != null and userType != ''" >
and user_type_ = #{userType,jdbcType=CHAR}
</if>
<if test="onlineStatus != null and onlineStatus != ''" >
and online_status_ = #{onlineStatus,jdbcType=CHAR}
</if>
<if test="loginUserStatus != null and loginUserStatus != ''" >
and login_user_status_ = #{loginUserStatus,jdbcType=CHAR}
</if>
<if test="lastLoginTime != null and lastLoginTime != ''" >
and last_login_time_ = #{lastLoginTime,jdbcType=VARCHAR}
</if>
<if test="pwErrorNumber != null and pwErrorNumber != ''" >
and pw_error_number_ = #{pwErrorNumber,jdbcType=CHAR}
</if>
</select>
</mapper>
mybatis-config.xml文件内容为:
<?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>
<!-- 登录用户表-->
<typeAlias alias="LoginUser" type="com.winphone.entity.LoginUser" />
</typeAliases>
<!-- 实体接口映射资源 -->
<plugins>
<plugin interceptor="com.winphone.common.PagePlugin">
<property name="dialect" value="mysql" />
<property name="pageSqlId" value=".*listPage.*" />
</plugin>
</plugins>
<!--
说明:如果xxMapper.xml配置文件放在和xxMapper.java统一目录下,mappers也可以省略,因为org.mybatis.spring.mapper.MapperFactoryBean默认会去查找与xxMapper.java相同目录和名称的xxMapper.xml
-->
<mappers>
<!-- 登录用户表-->
<mapper resource="mybatis/LoginUserMapper.xml" />
</mappers>
</configuration>
ApplicationContext-service.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">
<bean id="loginUserService" class="com.winphone.service.impl.LoginUserServiceImpl">
<property name="loginUserMapper" ref="loginUserMapper"></property>
</bean>
</beans>
四、修改文件ApplicationContext-mvc.xml、ApplicationContext.xml和web.xml
ApplicationContext-mvc.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:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-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/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
<!-- <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" >
<property name="messageConverters">
<list>
<bean class = "org.springframework.http.converter.StringHttpMessageConverter">
<property name = "supportedMediaTypes">
<list>
<value>text/html;charset=UTF-8</value>
</list>
</property>
</bean>
</list>
</property>
</bean> -->
<bean id = "stringHttpMessageConverter" class = "org.springframework.http.converter.StringHttpMessageConverter">
<property name = "supportedMediaTypes">
<list>
<value>text/html;charset=UTF-8</value>
</list>
</property>
</bean>
<bean id = "mappingJacksonHttpMessageConverter" class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter"/>
<!-- <bean id = "jsonHttpMessageConverter" class="org.springframework.http.converter.json.JsonHttpMessageConverter"/>
<bean id = "formHttpMessageConverter" class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter"/> -->
<!-- 启动Spring MVC的注解功能,完成请求和注解POJO的映射 -->
<bean class ="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" >
<property name= "messageConverters" >
<list>
<ref bean= "mappingJacksonHttpMessageConverter" />
<!-- 新增的StringMessageConverter bean-->
<ref bean= "stringHttpMessageConverter" />
<!-- <ref bean= "jsonHttpMessageConverter" />
<ref bean= "formHttpMessageConverter" /> -->
</list>
</property>
</bean>
<mvc:annotation-driven />
<!-- springmvc配置 -->
<!-- 通过component-scan让spring扫描package下的所有类,让spring的注解生效-->
<context:component-scan base-package="com.winphone"></context:component-scan>
<!-- 配置springmvc的视图渲染器,让其前缀为:/ 后缀为: .jsp 将视图渲染到 /views/<method返回值>.jsp中 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/view/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
</beans>
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:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<import resource="ApplicationContext-service.xml"/>
<context:property-placeholder location="classpath:properties/config-*.properties"/>
<!-- 数据源 -->
<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}" />
<property name="maxActive" value="${jdbc.maxActive}" />
<property name="maxIdle" value="${jdbc.maxIdle}" />
<property name="maxWait" value="${jdbc.maxWait}" />
<property name="removeAbandoned" value="${jdbc.removeAbandoned}" />
<property name="removeAbandonedTimeout" value="${jdbc.removeAbandonedTimeout}" />
<property name="logAbandoned" value="${jdbc.logAbandoned}" />
<property name="validationQuery" value="${jdbc.validationQuery}" />
<property name="testOnBorrow" value="${jdbc.testOnBorrow}" />
</bean>
<bean id="configProperties" class="org.springframework.beans.factory.config.PropertiesFactoryBean">
<property name="locations">
<list>
<value>classpath:properties/config-*.properties</value>
</list>
</property>
</bean>
<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PreferencesPlaceholderConfigurer">
<property name="properties" ref="configProperties" />
</bean>
<!--
mybatis的SqlSession的工厂: SqlSessionFactoryBean dataSource:引用数据源
MyBatis定义数据源,同意加载配置
-->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<property name="configLocation" value="classpath:mybatis/mybatis-config.xml" />
</bean>
<!--
mybatis自动扫描加载Sql映射文件/接口 : MapperScannerConfigurer sqlSessionFactory
basePackage:指定sql映射文件/接口所在的包(自动扫描)
-->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.winphone.mapper"></property>
<property name="sqlSessionFactory" ref="sqlSessionFactory"></property>
</bean>
<!--
事务管理 : DataSourceTransactionManager dataSource:引用上面定义的数据源
-->
<bean id="txManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean>
<!-- 使用声明式事务
transaction-manager:引用上面定义的事务管理器
-->
<tx:annotation-driven transaction-manager="txManager"/>
</beans>
web.xml文件内容为:
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
version="3.0">
<display-name>TestMaven</display-name>
<!-- 监听spring上下文容器 -->
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
<!-- 加载spring的xml配置文件到spring的上下文容器中 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring/ApplicationContext.xml</param-value>
</context-param>
<!-- 配置springmvc DispatcherServlet -->
<servlet>
<servlet-name>spring-mvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring/ApplicationContext-mvc.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>spring-mvc</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<session-config>
<session-timeout>30</session-timeout>
</session-config>
</web-app>
五、添加相应的实体、服务、接口、分页实体、分页插件实体、工具类
六、启动服务、页面显示从数据库中读取的数据