开发环境:
intellij idea:2018 + maven:3.5.4+ jdk:1.8
在这之前,需要在idea中配置好maven,没有配置的按照下述连接配置
https://blog.csdn.net/nba_linshuhao/article/details/82715485
下面正式开始配置过程
2、项目目录结构如下(三张图连着看),文件内容往下看
目录颜色不同看步骤3
3、让idea识别目录作用
file----project structure-----modelues
main包下面的java对应Sources,resources对应上面的Resources
test是测试,对应上面的Test Resources
设置之后颜色就跟步骤2的目录颜色一样了
3、pom.xml
直接复制文件到pom.xml中,然后maven会自动下载需要的jar包
(复制文件之后,右下角会提示选中第二个自动下载就好了)
4.0.0
com.ssm
ssm
war
1.0-SNAPSHOT
ChatRobot Maven Webapp
http://maven.apache.org
UTF-8
UTF-8
4.3.5.RELEASE
3.4.1
javax
javaee-api
7.0
junit
junit
4.12
ch.qos.logback
logback-classic
1.2.2
com.fasterxml.jackson.core
jackson-databind
2.8.7
mysql
mysql-connector-java
5.1.41
runtime
com.mchange
c3p0
0.9.5.2
org.mybatis
mybatis
${mybatis.version}
org.mybatis
mybatis-spring
1.3.1
org.springframework
spring-core
${spring.version}
org.springframework
spring-beans
${spring.version}
org.springframework
spring-context
${spring.version}
org.springframework
spring-jdbc
${spring.version}
org.springframework
spring-tx
${spring.version}
org.springframework
spring-web
${spring.version}
org.springframework
spring-webmvc
${spring.version}
org.springframework
spring-test
${spring.version}
ChatRobot
org.apache.maven.plugins
maven-compiler-plugin
1.8
4、logback.xml
%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n
5、jdbc.properties
jdbc.driver=com.mysql.jdbc.Driver
#数据库地址
jdbc.url=jdbc:mysql://localhost:3306/mytest?useUnicode=true&characterEncoding=utf8
#用户名
jdbc.username=root
#密码
jdbc.password=root
#最大连接数
c3p0.maxPoolSize=30
#最小连接数
c3p0.minPoolSize=10
#关闭连接后不自动commit
c3p0.autoCommitOnClose=false
#获取连接超时时间
c3p0.checkoutTimeout=10000
#当获取连接失败重试次数
c3p0.acquireRetryAttempts=2
6、spring-mybatis.xml
7、spring-mvc.xml
8、web.xml
index.html
ChatRobot
ChatRobot_Alpha_0.0.1
encodingFilter
org.springframework.web.filter.CharacterEncodingFilter
encoding
UTF-8
encodingFilter
/*
SpringMVC
org.springframework.web.servlet.DispatcherServlet
contextConfigLocation
classpath:spring-*.xml
1
true
SpringMVC
/
9、准备数据库--------mysql数据库,mysql用户名:root 密码:root
数据库名mytest、表名user
不一样的自行修改。
10、UserController.java
package com.ssm.controller;
import com.ssm.model.User;
import com.ssm.service.IUserService;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
@Controller
@RequestMapping("/user")
public class UserController {
@Resource
private IUserService userService;
@RequestMapping("/showUser.do")
public void selectUser(HttpServletRequest request, HttpServletResponse response) throws IOException {
request.setCharacterEncoding("UTF-8");
response.setCharacterEncoding("UTF-8");
long userId = Long.parseLong(request.getParameter("id"));
User user = this.userService.selectUser(userId);
ObjectMapper mapper = new ObjectMapper();
response.getWriter().write(mapper.writeValueAsString(user));
response.getWriter().close();
}
}
11、IUserDao.java
package com.ssm.dao;
import com.ssm.model.User;
public interface IUserDao {
User selectUser(long id);
}
12、User.java
package com.ssm.model;
import java.util.Date;
public class User {
private long id;
private String email;
private String password;
private String username;
private String role;
private int status;
private Date regTime;
private String regIp;
public long getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getRole() {
return role;
}
public void setRole(String role) {
this.role = role;
}
public int getStatus() {
return status;
}
public void setStatus(int status) {
this.status = status;
}
public Date getRegTime() {
return regTime;
}
public void setRegTime(Date regTime) {
this.regTime = regTime;
}
public String getRegIp() {
return regIp;
}
public void setRegIp(String regIp) {
this.regIp = regIp;
}
}
13、IUserService.java
package com.ssm.service;
import com.ssm.model.User;
public interface IUserService {
User selectUser(long userId);
int insert(User user);
}
14、UserServiceImpl.java
package com.ssm.service.impl;
import com.ssm.dao.IUserDao;
import com.ssm.model.User;
import com.ssm.service.IUserService;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
@Service("userService")
public class UserServiceImpl implements IUserService {
@Resource
private IUserDao userDao;
public User selectUser(long userId) {
return this.userDao.selectUser(userId);
}
@Override
public int insert(User user) {
return 0;
}
}
15、UserDao.xml
16、新建测试类,测试mybatis
IUserDaoTest.java
package com.ssm.daoTest;
import com.ssm.dao.IUserDao;
import com.ssm.model.User;
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;
// 加载spring配置文件
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration({"classpath:spring-mybatis.xml"})
public class IUserDaoTest {
@Autowired
private IUserDao dao;
@Test
public void testSelectUser() throws Exception {
long id = 1;
User user = dao.selectUser(id);
System.out.println(user.getUsername());
}
}
17、运行测试
上面的测试代码的目的是:从数据库中查询id为1的数据。所以运行测试之前需要在数据库中插入一两条数据,以免报错
18、新建index.html页面来测试SpringMVC和Mybatis
index.html
test
Hello World!
19、配置tomact,右上角edit configurations
21、还可以直接在地址栏中输入url,同样可以得到上面的结果
http://localhost:8080/user/showUser.do?id=1
可能出现的问题:
1、数据库名字密码错误
2、数据库中没有id为1的数据
3、mapper映射文件UserDao.xml中的namespace和包dao的路径不一样
整个项目的下载地址:
https://download.csdn.net/download/nba_linshuhao/10680908
Spring-mybatis的配置详解
https://blog.csdn.net/happy_wu/article/details/61216112