项目目录结构
maven配置文件:
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> 4.0.0 ssm ssm war 0.0.1-SNAPSHOT ssm Maven Webapp http://maven.apache.org 4.3.4.RELEASE 3.4.2 4.12 5.1.40 org.springframework spring-core ${spring.version} org.springframework spring-context ${spring.version} org.springframework spring-web ${spring.version} org.springframework spring-webmvc ${spring.version} org.springframework spring-jdbc ${spring.version} redis.clients jedis 2.8.1 org.springframework.data spring-data-redis 1.7.2.RELEASE org.springframework spring-oxm ${spring.version} org.springframework spring-test ${spring.version} org.springframework spring-context-support ${spring.version} org.springframework spring-aspects ${spring.version} aopalliance aopalliance 1.0 org.aspectj aspectjweaver 1.8.10 com.thetransactioncompany java-property-utils 1.9.1 com.google.code.gson gson 2.6.2 com.thetransactioncompany cors-filter 1.3.2 commons-fileupload commons-fileupload 1.3.2 org.apache.poi poi 3.15 com.fasterxml.jackson.core jackson-core 2.8.4 com.fasterxml.jackson.core jackson-databind 2.8.4 org.mybatis mybatis 3.4.2 org.mybatis mybatis-spring 1.3.1 com.github.pagehelper pagehelper 4.1.6 log4j log4j 1.2.17 junit junit ${junit.version} mysql mysql-connector-java 5.1.40 com.alibaba druid 1.0.29 org.apache.maven.plugins maven-compiler-plugin 3.6.1 1.8 1.8 org.mybatis.generator mybatis-generator-maven-plugin 1.3.5 true true
创建数据库 创建user表以mysql为例
CREATE TABLEuser ( id INTEGER PRIMARY KEY AUTO_INCREMENT, name CHAR(20 ) NOT NULL, passwordCHAR (40 ) NOT NULL) ENGINE = InnoDB AUTO_INCREMENT = 16 DEFAULT CHARSET = utf8;
添加user数据
INSERT INTO user (id,name, password) VALUES (1 ,'A' ,'AAA' ) ;
INSERT INTO user (id,name, password) VALUES (2 ,'B' ,'BBB' ) ;
配置mybatis文件 >>>> mybatis-config.xml
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
配置数据源 database.properties
db.driver=com.mysql.jdbc.Driver
db.url=jdbc:mysql://localhost:3306/ssm?characterEncoding=UTF-8
db.user=root
db.password=
db.maxActive=10
db.initialSize=2
db.minIdle=2
db.maxWait=60000
db.timeBetweenEvictionRunsMillis=3000
db.minEvictableIdleTimeMillis=300000
db.validationQuery=SELECT 'x' FROM DUAL
db.testWhileIdle=true
db.testOnBorrow=false
db.testOnReturn=false
Spring配置文件application.xml >>> 添加了spring-mybatis.xml
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">
创建
spring-mybatis.xml文件
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" 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/context http://www.springframework.org/schema/context/spring-context.xsd">
SpringMVC配置文件spring-mvc.xml
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"
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/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
配置log4j文件 >>>>
log4j.properties >>>> 输出sql日志
#将ibatis log4j运行级别调到DEBUG可以在控制台打印出ibatis运行的sql语句
log4j.rootLogger=debug,stdout,logfile ### 把日志信息输出到控制台 ### log4j.appender.stdout=org.apache.log4j.ConsoleAppender #log4j.appender.stdout.Target=System.err log4j.appender.stdout.layout=org.apache.log4j.SimpleLayout ### 把日志信息输出到文件:jbit.log ### log4j.appender.logfile=org.apache.log4j.FileAppender log4j.appender.logfile.File=D:/test.log log4j.appender.logfile.layout=org.apache.log4j.PatternLayout log4j.appender.logfile.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %F %p %m%n ###显示SQL语句部分 log4j.logger.com.ibatis=DEBUG log4j.logger.com.ibatis.common.jdbc.SimpleDataSource=DEBUG log4j.logger.com.ibatis.common.jdbc.ScriptRunner=DEBUG log4j.logger.com.ibatis.sqlmap.engine.impl.SqlMapClientDelegate=DEBUG log4j.logger.java.sql.Connection=DEBUG log4j.logger.java.sql.Statement=DEBUG log4j.logger.java.sql.PreparedStatement=DEBUG
MyBatis Generator配置文件generatorConfig.xml >>> 自动生成mybatis所需要的文件
PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN"
"http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd">
location="E:/repo/mysql/mysql-connector-java/5.1.40/mysql-connector-java-5.1.40.jar"/>
connectionURL="jdbc:mysql://localhost:3306/ssm"
userId="root"
password="">
进入项目文件夹运行maven命令
mvn mybatis-generator:generate
会生成mapper,bean,dao文件
web配置文件web.xml
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" version="3.1"> index.jsp 404 /error/404.jsp 405 /error/405.jsp 500 /error/500.jsp contextConfigLocation classpath:application.xml log4jConfigLocation classpath:properties/log4j.properties org.springframework.web.util.Log4jConfigListener SpringEncodingFilter org.springframework.web.filter.CharacterEncodingFilter encoding UTF-8 forceEncoding true SpringEncodingFilter /* 跨域过滤器 CORS com.thetransactioncompany.cors.CORSFilter cors.allowOrigin * cors.supportedMethods GET, POST, HEAD, PUT, DELETE cors.supportedHeaders Accept, Origin, X-Requested-With, Content-Type, Last-Modified cors.exposedHeaders Set-Cookie cors.supportsCredentials true CORS /* org.springframework.web.context.ContextLoaderListener dispatcher org.springframework.web.servlet.DispatcherServlet contextConfigLocation classpath:spring/spring-mvc.xml 1 dispatcher /
接口和类
com.demo.bean下的User.java和UserExample.java
这两个类是由MBG生成的
com.demo.dao下的UserMapper.java
也由MBG生成的
com.demo.service下的UserService.java接口 package com.demo.service;
import java.util.List;
import com.demo.bean.User;
public interface UserService {
void insertUser(User user);
void deleteUser(int id);
User findUserById(Integer id);
List findAllUsers();
void updateUser(User user);
}
实现userSerivce接口
package com.demo.service.impl; import java.util.List; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import com.demo.bean.User; import com.demo.bean.UserExample; import com.demo.dao.UserMapper; import com.demo.service.UserService; @Service public class UserServiceImpl implements UserService { @Autowired private UserMapper userMapper; public void insertUser(User user) { userMapper.insert(user); } public void deleteUser(int id) { UserExample userExample = new UserExample(); userExample.createCriteria().andIdEqualTo(id); userMapper.deleteByExample(userExample); } public User findUserById(Integer id) { return userMapper.selectByPrimaryKey(id); } public List findAllUsers() { return userMapper.selectByExample(null); } public void updateUser(User user) { userMapper.updateByPrimaryKey(user); } }
com.demo.controller下的UserController.java
MVC模式下的控制器,通过注入的UserService完成相关业务
package com.demo.controller;
import java.util.List;
import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import com.demo.bean.User;
import com.demo.service.UserService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
@Controller
@RequestMapping(value = "/user")
public class UserController {
private static Logger logger = LogManager.getLogger(User.class);
@Autowired
private UserService userService;
@RequestMapping(value = "/{id}", method = RequestMethod.GET)
@ResponseBody
public User findUserById(@PathVariable("id") Integer id) {
User user = userService.findUserById(id);
logger.info(user.toString());
return user;
}
@RequestMapping(value = "/list", method = RequestMethod.GET)
@ResponseBody
public List
getListUset(){
PageHelper.startPage(1, 10);
List list = userService.findAllUsers();
PageInfo pageInfo = new PageInfo(list);
System.out.println("++++++++++++++++++++++++++++++++++++++++++++++");
System.out.println(pageInfo);
return list;
}
}
测试>>> 将项目部署到tomcat
http:
源码URL : http://download.csdn.net/detail/a295277302/9818445