项目源码下载
jdk
java version "1.8.0_131"
Java(TM) SE Runtime Environment (build 1.8.0_131-b11)
Java HotSpot(TM) 64-Bit Server VM (build 25.131-b11, mixed mode)
Maven
apache-tomcat-7.0.64
CREATE DATABASE `mybatis` ;
USE `mybatis`;
DROP TABLE IF EXISTS `users`;
CREATE TABLE `users` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`NAME` varchar(20) DEFAULT NULL,
`age` int(11) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=37 DEFAULT CHARSET=utf8;
需要的jar包
<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.0modelVersion>
<groupId>com.xwxgroupId>
<artifactId>MavenDemoartifactId>
<version>0.0.1-SNAPSHOTversion>
<packaging>warpackaging>
<properties>
<project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
<spring.version>4.3.6.RELEASEspring.version>
properties>
<dependencies>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-coreartifactId>
<version>${spring.version}version>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-beansartifactId>
<version>${spring.version}version>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-contextartifactId>
<version>${spring.version}version>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-txartifactId>
<version>${spring.version}version>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-webartifactId>
<version>${spring.version}version>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-webmvcartifactId>
<version>${spring.version}version>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-aopartifactId>
<version>${spring.version}version>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-expressionartifactId>
<version>${spring.version}version>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-jdbcartifactId>
<version>${spring.version}version>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-testartifactId>
<version>${spring.version}version>
dependency>
<dependency>
<groupId>org.mybatisgroupId>
<artifactId>mybatisartifactId>
<version>3.4.5version>
dependency>
<dependency>
<groupId>org.mybatisgroupId>
<artifactId>mybatis-springartifactId>
<version>1.3.1version>
dependency>
<dependency>
<groupId>mysqlgroupId>
<artifactId>mysql-connector-javaartifactId>
<version>5.1.44version>
dependency>
<dependency>
<groupId>com.alibabagroupId>
<artifactId>druidartifactId>
<version>1.0.28version>
dependency>
<dependency>
<groupId>commons-logginggroupId>
<artifactId>commons-loggingartifactId>
<version>1.2version>
dependency>
<dependency>
<groupId>junitgroupId>
<artifactId>junitartifactId>
<version>4.12version>
<scope>testscope>
dependency>
<dependency>
<groupId>org.aspectjgroupId>
<artifactId>aspectjweaverartifactId>
<version>1.8.10version>
dependency>
<dependency>
<groupId>commons-fileuploadgroupId>
<artifactId>commons-fileuploadartifactId>
<version>1.3.3version>
dependency>
<dependency>
<groupId>javax.servletgroupId>
<artifactId>servlet-apiartifactId>
<version>3.0-alpha-1version>
dependency>
<dependency>
<groupId>org.codehaus.jacksongroupId>
<artifactId>jackson-core-aslartifactId>
<version>1.9.13version>
dependency>
<dependency>
<groupId>org.codehaus.jacksongroupId>
<artifactId>jackson-mapper-aslartifactId>
<version>1.9.13version>
dependency>
<dependency>
<groupId>log4jgroupId>
<artifactId>log4jartifactId>
<version>1.2.17version>
dependency>
<dependency>
<groupId>taglibsgroupId>
<artifactId>standardartifactId>
<version>1.1.2version>
dependency>
<dependency>
<groupId>jstlgroupId>
<artifactId>jstlartifactId>
<version>1.1.2version>
dependency>
<dependency>
<groupId>com.github.pagehelpergroupId>
<artifactId>pagehelperartifactId>
<version>4.0.0version>
dependency>
<dependency>
<groupId>org.hibernategroupId>
<artifactId>hibernate-validatorartifactId>
<version>5.3.1.Finalversion>
dependency>
<dependency>
<groupId>javax.validationgroupId>
<artifactId>validation-apiartifactId>
<version>2.0.0.Finalversion>
dependency>
<dependency>
<groupId>org.jboss.logginggroupId>
<artifactId>jboss-loggingartifactId>
<version>3.3.0.Finalversion>
dependency>
dependencies>
project>
<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"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<context:property-placeholder location="classpath:config.properties"/>
<context:component-scan base-package="com.xwx.service" />
beans>
<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:mvc="http://www.springframework.org/schema/mvc"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<context:component-scan base-package="com.xwx.controller" />
<mvc:annotation-driven validator="msgValidator" message-codes-resolver="messageCodeResolver">mvc:annotation-driven>
<bean id="messageCodeResolver" class="org.springframework.validation.DefaultMessageCodesResolver">
<property name="prefix" value="validation.">property>
bean>
<bean id="msgValidator" class="org.springframework.validation.beanvalidation.LocalValidatorFactoryBean">
<property name="providerClass" value="org.hibernate.validator.HibernateValidator"/>
<property name="validationMessageSource" ref="validationMessageSource"/>
bean>
<bean id="validationMessageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
<property name="basenames">
<list>
<value>classpath:messagesvalue>
<value>classpath:hibernate/validator/ValidationMessagesvalue>
list>
property>
<property name="useCodeAsDefaultMessage" value="false"/>
<property name="defaultEncoding" value="UTF-8"/>
<property name="cacheSeconds" value="60"/>
bean>
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" p:prefix="/WEB-INF/jsp/" p:suffix=".jsp" />
beans>
<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"
xsi:schemaLocation="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.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<bean name="dataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close">
<property name="url" value="${jdbc_url}"/>
<property name="username" value="${jdbc_username}"/>
<property name="password" value="${jdbc_password}"/>
<property name="initialSize" value="0"/>
<property name="maxActive" value="20"/>
<property name="maxIdle" value="20"/>
<property name="minIdle" value="0"/>
<property name="maxWait" value="60000" />
<property name="validationQuery" value="${validationQuery}" />
<property name="testOnBorrow" value="false" />
bean>
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="mapperLocations" value="classpath:com/xwx/mapping/*.xml"/>
<property name="configLocation" value="classpath:mybatisPlugin.xml" />
bean>
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.xwx.dao" />
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
bean>
beans>
driverClassName=com.mysql.jdbc.Driver
jdbc_url=jdbc:mysql://localhost/mybatis?useUnicode=true&characterEncoding=UTF-8
jdbc_username=root
jdbc_password=root
validationQuery=select 1
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" 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>MavenDemodisplay-name>
<context-param>
<param-name>contextConfigLocationparam-name>
<param-value>classpath:spring.xml,classpath:spring-mybatis.xmlparam-value>
context-param>
<filter>
<description>字符集过滤器description>
<filter-name>characterEncodingFilterfilter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilterfilter-class>
<init-param>
<param-name>encodingparam-name>
<param-value>UTF-8param-value>
init-param>
<init-param>
<param-name>forceEncodingparam-name>
<param-value>trueparam-value>
init-param>
filter>
<filter-mapping>
<filter-name>characterEncodingFilterfilter-name>
<url-pattern>/*url-pattern>
filter-mapping>
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
listener-class>
listener>
<servlet>
<servlet-name>DispatcherServletservlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
servlet-class>
<init-param>
<param-name>contextConfigLocationparam-name>
<param-value>classpath:spring-mvc.xmlparam-value>
init-param>
<load-on-startup>1load-on-startup>
servlet>
<servlet-mapping>
<servlet-name>DispatcherServletservlet-name>
<url-pattern>*.dourl-pattern>
servlet-mapping>
<session-config>
<session-timeout>120session-timeout>
session-config>
<error-page>
<error-code>404error-code>
<location>/404.jsplocation>
error-page>
<welcome-file-list>
<welcome-file>index.htmlwelcome-file>
<welcome-file>index.htmwelcome-file>
<welcome-file>index.jspwelcome-file>
<welcome-file>default.htmlwelcome-file>
<welcome-file>default.htmwelcome-file>
<welcome-file>default.jspwelcome-file>
welcome-file-list>
web-app>
maven项目引入log4j的详细配置
@Controller @RequestMapping @Service @Autowired
package com.xwx.controller;
import java.util.List;
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.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.validation.ObjectError;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.xwx.model.User;
import com.xwx.service.UserService;
@Controller
@RequestMapping("/user")
public class UserController {
//设置log
private static Logger log = Logger.getLogger(UserController.class.getName());
@Autowired
private UserService userService;
public UserService getUser() {
return userService;
}
public void setUser(UserService user) {
userService = user;
}
@RequestMapping(value="/index", method={RequestMethod.GET})
public String index(HttpServletRequest request, ModelMap modelMap, @RequestParam(value="name", required=false) String name, @RequestParam(value="page", defaultValue="1") String paramPage){
if(null == paramPage){
paramPage = "1";
}
int page = Integer.parseInt(paramPage);
int pageSize = 3;
log.debug("name="+name);
log.debug("page"+page);
PageHelper.startPage(page, pageSize);
PageHelper.orderBy("id desc");
List<User> u = null;
if(null !=name && !name.equals("")){
User user = new User();
user.setName(name);
u = userService.selectAll(user);
} else {
u = userService.selectAll(null);
}
PageInfo<User> pageInfo = new PageInfo<User>(u);
log.debug(u);
log.debug(pageInfo);
modelMap.addAttribute("user", u);
modelMap.addAttribute("name", name);
modelMap.addAttribute("pageInfo", pageInfo.getNavigatepageNums());
return "user/index";
}
@RequestMapping("/update")
public String update(int id, HttpServletRequest request, ModelMap modelMap){
User user = userService.getUserById(id);
modelMap.addAttribute("user", user);
log.debug(user);
return "user/update";
}
@RequestMapping("/doUpdate")
public String doUpdate(HttpServletRequest request, ModelMap modelMap, @Validated User user, BindingResult bindingResult){
if(bindingResult.hasErrors()){
List<ObjectError> errors = bindingResult.getAllErrors();
log.error(bindingResult.getAllErrors().toString());
modelMap.addAttribute("errors",errors);
return "user/update";
}
userService.updateByPrimaryKey(user);
return "redirect:index.do";
}
@RequestMapping("/add")
public String add(){
return "user/add";
}
@RequestMapping("/doAdd")
public String doAdd(HttpServletRequest request, ModelMap modelMap, @Validated User user, BindingResult bindingResult) {
log.debug(user);
if(bindingResult.hasErrors()){
List<ObjectError> errors = bindingResult.getAllErrors();
log.error(bindingResult.getAllErrors().toString());
modelMap.addAttribute("errors",errors);
return "user/add";
}
userService.insert(user);
return "redirect:index.do";
}
@RequestMapping("/delete")
public String delete(int id){
userService.deleteByPrimaryKey(id);
return "redirect:index.do";
}
}
package com.xwx.service;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.xwx.dao.UserMapper;
import com.xwx.model.User;
@Service("userService")
public class UserService {
@Autowired
private UserMapper UserMapper;
public UserMapper getUserMapper() {
return UserMapper;
}
public void setUserMapper(UserMapper userMapper) {
UserMapper = userMapper;
}
public User getUserById(int id){
User u = UserMapper.selectByPrimaryKey(id);
return u;
}
public int updateByPrimaryKey(User user){
return UserMapper.updateByPrimaryKey(user);
}
public List<User> selectAll(User user) {
return UserMapper.selectAll(user);
}
public int insert(User user){
return UserMapper.insert(user);
}
public int deleteByPrimaryKey(int id){
return UserMapper.deleteByPrimaryKey(id);
}
}