2019独角兽企业重金招聘Python工程师标准>>>
Spring
Spring是一个开源框架,Spring是于2003 年兴起的一个轻量级的Java 开发框架,由Rod Johnson 在其著作Expert One-On-One J2EE Development and Design中阐述的部分理念和原型衍生而来。它是为了解决企业应用开发的复杂性而创建的。Spring使用基本的JavaBean来完成以前只可能由EJB完成的事情。然而,Spring的用途不仅限于服务器端的开发。从简单性、可测试性和松耦合的角度而言,任何Java应用都可以从Spring中受益。 简单来说,Spring是一个轻量级的控制反转(IOC)和面向切面(AOP)的容器框架。
###** SpringMVC** Spring MVC属于SpringFrameWork的后续产品,已经融合在Spring Web Flow里面。Spring MVC 分离了控制器、模型对象、分派器以及处理程序对象的角色,这种分离让它们更容易进行定制。
###** MyBatis** MyBatis 本是apache的一个开源项目iBatis, 2010年这个项目由apache software foundation 迁移到了google code,并且改名为MyBatis 。MyBatis是一个基于Java的持久层框架。iBATIS提供的持久层框架包括SQL Maps和Data Access Objects(DAO)MyBatis 消除了几乎所有的JDBC代码和参数的手工设置以及结果集的检索。MyBatis 使用简单的 XML或注解用于配置和原始映射,将接口和 Java 的POJOs(Plain Old Java Objects,普通的 Java对象)映射成数据库中的记录。
开发环境搭建
- jdk1.7及以上
- apache-maven-3.3.9
- apache-tomcat-8.0.9
- 开发工具IDEA
整合SSM框架
- 新建maven工程,目录如下:
- 建立MVC分层
3.resources文件夹下创建资源文件
详细配置
1.Maven引入需要的JAR包
jar包用途注释有说明
4.0.0
com.test
ssmDemo
war
1.0-SNAPSHOT
ssmDemo
http://maven.apache.org
UTF-8
4.3.6.RELEASE
junit
junit
4.12
test
org.springframework
spring-core
${spring.version}
org.springframework
spring-webmvc
${spring.version}
org.springframework
spring-context
${spring.version}
org.springframework
spring-tx
${spring.version}
org.springframework
spring-jdbc
${spring.version}
org.springframework
spring-dao
2.0.8
org.aspectj
aspectjweaver
1.8.6
com.alibaba
druid
1.0.26
mysql
mysql-connector-java
6.0.5
org.mybatis
mybatis
3.4.1
org.mybatis
mybatis-spring
1.3.0
log4j
log4j
1.2.17
commons-logging
commons-logging
1.2
org.slf4j
slf4j-log4j12
1.7.21
ssmDemo
src/main/resources
**/*.properties
**/*.xml
true
src/main/java
**/*.properties
**/*.xml
true
org.apache.maven.plugins
maven-compiler-plugin
3.1
1.7
UTF-8
org.mybatis.generator
mybatis-generator-maven-plugin
1.3.2
true
true
2.web.xml配置
CharacterEncodingFilter
org.springframework.web.filter.CharacterEncodingFilter
encoding
UTF-8
forceEncoding
true
CharacterEncodingFilter
/*
ssmDemo
org.springframework.web.servlet.DispatcherServlet
contextConfigLocation
classpath:applicationContext.xml
1
ssmDemo
/*
webAppRootKey
ssmDemo.root
log4jRefreshInterval
10000
org.springframework.web.util.Log4jConfigListener
3.applicationContext.xml配置
4.dataSource.properties配置
driverClassName=com.mysql.jdbc.Driver
jdbc.connectionURL=jdbc:mysql://127.0.0.1:3306/test?useUnicode=true&characterEncoding=UTF-8&zeroDateTimeBehavior=convertToNull&serverTimezone=UTC
jdbc.username=root
jdbc.password=123456
#
initialSize=0
#
maxActive=20
#
minIdle=0
#
maxWait=60000
#
minEvictableIdleTimeMillis=25200000
#
removeAbandonedTimeout=1800
5.log4j.properties配置
log4j.rootLogger=info , stdout, D, E
log4j.logger.java.sql.Connection = DEBUG
log4j.logger.java.sql.Statement = DEBUG
log4j.logger.java.sql.PreparedStatement = DEBUG
log4j.logger.java.sql.ResultSet = DEBUG
log4j.appender.stdout = org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target = System.out
log4j.appender.stdout.layout = org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern = %-d{yyyy-MM-dd HH:mm:ss} [ %t:%r ] - [ %p ] %m%n
#文件大小到达指定尺寸的时候产生一个新的文件
log4j.appender.D = org.apache.log4j.RollingFileAppender
#指定debug.log文件输出地
log4j.appender.D.File = ${YHHSMS.root}/WEB-INF/logs/debug.log
#指定每个log.debug文件大小为20k,建议设置成20M
log4j.appender.D.MaxFileSize = 20MB
#备份log文件最多为10个,即debug.log10,之后还需要记录日志的话,将重新覆盖debug.log,debug.log.1 ...
log4j.appender.D.MaxBackupIndex=10
#true意思:将消息增加到指定文件中,false指将消息覆盖指定的文件内容
log4j.appender.D.Append = true
#消息输出层次
log4j.appender.D.Threshold = DEBUG
#消息输出样式
log4j.appender.D.layout = org.apache.log4j.PatternLayout
#消息输出格式
log4j.appender.D.layout.ConversionPattern = %-d{yyyy-MM-dd HH:mm:ss} [ %t:%r ] - [ %p ] %m%n
#每天产生一个日志文件
log4j.appender.E = org.apache.log4j.DailyRollingFileAppender
#指定debug.log文件输出地
log4j.appender.E.File = ${YHHSMS.root}/WEB-INF/logs/error.log
#每天产生一个新的日志
log4j.appender.E.DatePattern = "."yyyy-MM-dd
#true意思:将消息增加到指定文件中,false指将消息覆盖指定的文件内容
log4j.appender.E.Append = true
#消息输出层次
log4j.appender.E.Threshold = ERROR
#消息输出样式
log4j.appender.E.layout = org.apache.log4j.PatternLayout
#消息输出格式
log4j.appender.E.layout.ConversionPattern = %-d{yyyy-MM-dd HH:mm:ss} [ %t:%r ] - [ %p ] %m%n
6.spring-mvc.xml配置
7.spring-mybatis.xml配置
8.generator.properties和generatorConfig.xml配置
PS:基于Maven的Mybatis_generator生成mapper,model 自行百度
关于插件方式生成mapper,model请参考https://my.oschina.net/u/2963821/blog/760035
数据库设计
编码设计
实现简单的用户列表查询
1.Controller
package com.test.ssmDemo.controller;
import com.test.ssmDemo.service.UserService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class UserController {
@Autowired
private UserService userService;
public static final Logger logger;
static {
logger = LoggerFactory.getLogger(UserController.class);
}
@RequestMapping(value = "/getUserList", method = RequestMethod.GET)
public void getUserList() {
logger.info(userService.getUserList().toString());
}
}
2.Service
package com.test.ssmDemo.controller;
import com.test.ssmDemo.service.UserService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class UserController {
@Autowired
private UserService userService;
public static final Logger logger;
static {
logger = LoggerFactory.getLogger(UserController.class);
}
@RequestMapping(value = "/getUserList", method = RequestMethod.GET)
public void getUserList() {
logger.info(userService.getUserList().toString());
}
}
3.ServiceImpl
package com.test.ssmDemo.service.impl;
import com.test.ssmDemo.mapper.UserMapper;
import com.test.ssmDemo.model.User;
import com.test.ssmDemo.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class UserServiceImpl implements UserService {
@Autowired
private UserMapper userMapper;
@Override
public List getUserList() {
return userMapper.selectUserList();
}
}
4.UserMapper.java
package com.test.ssmDemo.mapper;
import com.test.ssmDemo.model.User;
import java.util.List;
public interface UserMapper {
int deleteByPrimaryKey(String userId);
int insert(User record);
int insertSelective(User record);
User selectByPrimaryKey(String userId);
int updateByPrimaryKeySelective(User record);
int updateByPrimaryKey(User record);
List selectUserList();
}
5.UserMapper.xml
user_id, name, age, tel
delete from user
where user_id = #{userId,jdbcType=VARCHAR}
insert into user (user_id, name, age,
tel)
values (#{userId,jdbcType=VARCHAR}, #{name,jdbcType=VARCHAR}, #{age,jdbcType=INTEGER},
#{tel,jdbcType=VARCHAR})
insert into user
user_id,
name,
age,
tel,
#{userId,jdbcType=VARCHAR},
#{name,jdbcType=VARCHAR},
#{age,jdbcType=INTEGER},
#{tel,jdbcType=VARCHAR},
update user
name = #{name,jdbcType=VARCHAR},
age = #{age,jdbcType=INTEGER},
tel = #{tel,jdbcType=VARCHAR},
where user_id = #{userId,jdbcType=VARCHAR}
update user
set name = #{name,jdbcType=VARCHAR},
age = #{age,jdbcType=INTEGER},
tel = #{tel,jdbcType=VARCHAR}
where user_id = #{userId,jdbcType=VARCHAR}
最后把项目跑起来
效果如图: