Spring+SpringMVC+MyBatis+maven
spring-5.1.2.RELEASE
mybatis-3.4.6
jdk1.8+
搭建环境
1. pom.xml配置引入jar包
UTF-8
UTF-8
5.1.2.RELEASE
3.4.6
1.7.25
1.2.17
org.apache.maven.plugins
maven-compiler-plugin
3.7.0
1.8
UTF-8
org.apache.tomcat.maven
tomcat7-maven-plugin
2.2
org.apache.maven.plugins
maven-surefire-plugin
2.19.1
true
org.springframework
spring-context-support
${spring.version}
org.springframework
spring-jms
${spring.version}
org.springframework
spring-orm
${spring.version}
org.springframework
spring-oxm
${spring.version}
org.springframework
spring-test
${spring.version}
org.springframework
spring-webmvc
${spring.version}
org.springframework
spring-aspects
${spring.version}
aopalliance
aopalliance
1.0
org.mybatis
mybatis
${mybatis.version}
cglib
cglib
3.2.9
commons-logging
commons-logging
1.2
ognl
ognl
3.2.8
com.github.pagehelper
pagehelper
5.1.8
tk.mybatis
mapper
4.1.0
org.mybatis
mybatis-spring
1.3.2
org.mybatis.generator
mybatis-generator-core
1.3.7
com.oracle
ojdbc6
11.2.0
javax
javaee-api
8.0
provided
commons-dbcp
commons-dbcp
1.4
com.mchange
c3p0
0.9.5.2
org.hibernate
hibernate-c3p0
5.2.2.Final
jstl
jstl
1.2
org.apache.logging.log4j
log4j-core
2.9.1
org.slf4j
slf4j-log4j12
${slf4j.version}
com.alibaba
fastjson
1.2.53
com.fasterxml.jackson.core
jackson-databind
2.9.7
commons-fileupload
commons-fileupload
1.3.3
commons-io
commons-io
2.6
commons-codec
commons-codec
1.11
javax.servlet
javax.servlet-api
3.1.0
provided
javax.servlet.jsp
javax.servlet.jsp-api
2.3.1
provided
junit
junit
4.12
test
org.hibernate
hibernate-validator
5.3.5.Final
2. 数据库配置(使用默认配置)
- db.properties
dataSource.driverClass=oracle.jdbc.driver.OracleDriver
dataSource.jdbcUrl=jdbc\:oracle\:thin\:@localhost\:1521/ssm
dataSource.user=system
dataSource.password=manager
dataSource.maxPoolSize=20
dataSource.maxIdleTime = 1000
dataSource.minPoolSize=6
dataSource.initialPoolSize=5
3. spring配置
- applicationContext.xml
4.springMVC配置
- springmvc-config.xml
text/html;charset=UTF-8
application/json;charset=UTF-8
application/xml;charset=UTF-8
/
.jsp
5.web.xml配置
org.springframework.web.context.ContextLoaderListener
contextConfigLocation
classpath:applicationContext.xml
springmvc
org.springframework.web.servlet.DispatcherServlet
contextConfigLocation
classpath:springmvc-config.xml
1
springmvc
/
characterEncodingFilter
org.springframework.web.filter.CharacterEncodingFilter
encoding
UTF-8
characterEncodingFilter
/*
6. 持久层
- WebBook.java
public class WebBook {
private Integer id;
private String name;
private String author;
private Date publicationdate;
private String publication;
private Double price;
private String image;
private String remark;
}
- WebUser.java
public class WebUser {
private Integer id;
private String username;
private String loginname;
private String password;
private String phone;
private String address;
}
- WebUserMapper.java
public interface WebUserMapper {
/**
* 根据登录名和密码查询用户
* @param loginname
* @param password
* @return 找到返回user 没有返回null
*/
@Select("select * from web_user where loginname = #{loginname} and password = #{password} ")
WebUser selectWithLoginnameAndPassword(@Param("loginname") String loginname,@Param("password") String password);
}
- WebBookMapper.java
public interface WebBookMapper {
/**
* 查询所有图书
* @return 所有图书对象集合
*/
@Select("select * from web_book ")
List findAll();
}
7. 服务层
- 接口类WebUserService.java
public interface WebUserService {
/**
* 判断用户是否登录
* @param loginname
* @param password
* @return 找到返回user对象,没找到返回null
*/
WebUser login(String loginname,String password);
}
- 实现类WebUserServiceImpl.java
/**
* 服务层接口实现类
* @Service("userService")用于将当前类注释为一个Spring的bean,名为userService
* @author zgldo
*
*/
@Service("userService")
public class WebUserServiceImpl implements WebUserService {
@Autowired
private WebUserMapper mapper;
@Override
public WebUser login(String loginname, String password) {
return mapper.selectWithLoginnameAndPassword(loginname, password);
}
}
- 接口类WebBookService.java
public interface WebBookService {
/**
* 查找所有图书
* @return 图书对象集合
*/
List getAll();
}
- 实现类WebBookServiceImpl.java
@Service("bookService")
public class WebBookServiceImpl implements WebBookService {
@Autowired
private WebBookMapper mapper;
@Override
public List getAll() {
return this.mapper.findAll();
}
}
8. 控制层
- FormController.java
/**
* 动态页面跳转
* @author zgldo
*
*/
@Controller
public class FormController {
@RequestMapping(value="/{formName}")
public String loginForm(@PathVariable String formName) {
return formName;
}
}
- UserController.java
@Controller
public class UserController {
@Autowired
@Qualifier("userService")
private WebUserService userService;
@RequestMapping(value="/login")
public ModelAndView login(String loginname,String password,ModelAndView mv,HttpSession session) {
WebUser user = this.userService.login(loginname, password);
if(user != null) {
//登录成功,将user对象设置到Httpsession作用域范围
session.setAttribute("user", user);
//转发请求
//mv.setViewName(new RedirectView("/main"));
mv.setViewName("redirect:main");
}else {
mv.addObject("message", "登录名或密码错误,请重新输入!");
mv.setViewName("loginForm");
}
return mv;
}
}
- BookController.java
@Controller
public class BookController {
@Autowired
@Qualifier("bookService")
private WebBookService bookService;
@RequestMapping(value="/main")
public String main(Model model) {
List books = this.bookService.getAll();
model.addAttribute("books",books);
return "main";
}
}
9. jsp页面
- loginForm.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
登录页面
登录页面
- main.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
首页
欢迎[${sessionScope.user.username }]访问
封面 书名 作者 价格
${book.name }
${book.author }
${book.price }
10. 测试
部署到tomcat后,通过浏览器访问http://localhost:8080/maven-ssm/loginForm
输入用户名后,正常登陆,跳转页面:
[图片上传失败...(image-b7c658-1543768989991)]
登陆失败: