百度都找得到,这里不细说。
百度都找得到,这里不细说。
4.3.14.RELEASE
3.4.1
1.7.7
1.2.17
junit
junit
4.12
test
org.springframework
spring-webmvc
${spring.version}
org.springframework
spring-jdbc
${spring.version}
org.springframework
spring-test
${spring.version}
org.mybatis
mybatis
${mybatis.version}
org.mybatis
mybatis-spring
1.3.0
javax
javaee-api
7.0
mysql
mysql-connector-java
5.1.38
commons-dbcp
commons-dbcp
1.2.2
jstl
jstl
1.2
log4j
log4j
${log4j.version}
com.alibaba
fastjson
1.1.41
org.slf4j
slf4j-api
${slf4j.version}
org.slf4j
slf4j-log4j12
${slf4j.version}
org.codehaus.jackson
jackson-mapper-asl
1.9.13
ssm
1)建立JDBC属性文件jdbc.properties (文件编码修改为 utf-8 )
driverClassName=com.mysql.jdbc.Driver
jdbc_url=jdbc:mysql://localhost:3306/spring?useUnicode=true&characterEncoding=UTF-8
jdbc_username=root
jdbc_password=123456
#定义初始连接数
initialSize=0
#定义最大连接数
maxActive=20
#定义最大空闲
maxIdle=20
#定义最小空闲
minIdle=1
#定义最长等待时间
maxWait=60000
2)建立spring-mybatis.xml配置文件
3)Log4j的配置
#定义LOG输出级别
log4j.rootLogger=INFO,Console,File
#定义日志输出目的地为控制台
log4j.appender.Console=org.apache.log4j.ConsoleAppender
log4j.appender.Console.Target=System.out
#可以灵活地指定日志输出格式,下面一行是指定具体的格式
log4j.appender.Console.layout = org.apache.log4j.PatternLayout
log4j.appender.Console.layout.ConversionPattern=[%c] - %m%n
#文件大小到达指定尺寸的时候产生一个新的文件
log4j.appender.File = org.apache.log4j.RollingFileAppender
#指定输出目录
log4j.appender.File.File = logs/ssm.log
#定义文件最大大小
log4j.appender.File.MaxFileSize = 10MB
# 输出所以日志,如果换成DEBUG表示输出DEBUG以上级别日志
log4j.appender.File.Threshold = ALL
log4j.appender.File.layout = org.apache.log4j.PatternLayout
log4j.appender.File.layout.ConversionPattern =[%p] [%d{yyyy-MM-dd HH\:mm\:ss}][%c]%m%n
4)JUnit测试
①创建测试用表
本文使用mysql数据库
CREATE TABLE `user` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`user_name` varchar(40) NOT NULL,
`password` varchar(255) NOT NULL,
`age` int(4) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8;
/*Data for the table `user` */
insert into `user`(`id`,`user_name`,`password`,`age`) values (1,'你好','sfasgfaf',24);
②利用MyBatis Generator自动创建代码
参考博客:https://blog.csdn.net/qq_33269520/article/details/79785214
自动生成 User.java,UserMapper,IUserDao.java 三个文件,拷到项目中来,
下面给出service的接口和实现类代码:
service:
package com.lsy.ssm.service;
import com.lsy.ssm.pojo.User;
public interface IUserService {
public User getUserById(int userId);
}
serviceImpl:
package com.lsy.ssm.serviceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.lsy.ssm.dao.IUserDao;
import com.lsy.ssm.pojo.User;
import com.lsy.ssm.service.IUserService;
@Service
public class UserServiceImpl implements IUserService {
@Autowired
private IUserDao userDao;
public User getUserById(int userId) {
return this.userDao.selectByPrimaryKey(userId);
}
}
③建立测试类
MybatisTest:
package com.lsy.ssm;
import org.apache.log4j.Logger;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import com.alibaba.fastjson.JSON;
import com.lsy.ssm.pojo.User;
import com.lsy.ssm.service.IUserService;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "classpath:spring-mybatis.xml" })
public class MybatisTest {
public static Logger logger = Logger.getLogger(MybatisTest.class);
@Autowired
private IUserService userService;
@Test
public void testMybatis(){
User user = userService.getUserById(1);
logger.info(JSON.toJSONString(user));
}
}
至此, 完成Spring和mybatis这两大框架的整合 ,下面在继续进行SpringMVC的整合。
1)配置spring-mvc.xml
2)配置web.xml文件
Archetype Created Web Application
contextConfigLocation
classpath:spring-mybatis.xml
encodingFilter
org.springframework.web.filter.CharacterEncodingFilter
true
encoding
UTF-8
encodingFilter
/*
org.springframework.web.context.ContextLoaderListener
org.springframework.web.util.IntrospectorCleanupListener
SpringMVC
org.springframework.web.servlet.DispatcherServlet
contextConfigLocation
classpath:spring-mvc.xml
1
true
SpringMVC
/
/index.jsp
3)测试
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
test
${user.userName}
②建立UserController类
UserController.java
package com.lsy.ssm.controller;
import javax.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import com.lsy.ssm.pojo.User;
import com.lsy.ssm.service.IUserService;
@Controller
@RequestMapping("/user")
public class UserController {
@Autowired
private IUserService userService;
@RequestMapping("/showUser")
public String toIndex(HttpServletRequest request, Model model ){
int userId = Integer.parseInt(request.getParameter("id"));
User user = userService.getUserById(userId);
model.addAttribute("user", user);
return "showUser";
}
}
③tomcat部署项目
输入地址: localhost:8080/ 项目名称 /user/showUser?id=1
至此,SSM三大框架的整合就完成了,在此基础上可再集成其他功能。