前言:
本文章适用于在Eclipse上新建一个Maven Web SSM整合的项目,并显示Hello World页面以及连接数据库。
项目搭建:
File → New → Maven Project → Next
选择 maven-archetype-webapp
Group Id:一般填写域名 com.spring
Artifact Id: 一般填写项目名 springdemo
pom.xml
4.0.0
com.spring
springdemo
war
0.0.1-SNAPSHOT
springdemo Maven Webapp
http://maven.apache.org
2.6
1.7.6
4.1.3.RELEASE
org.springframework
spring-framework-bom
${spring.version}
pom
import
org.springframework
spring-test
org.springframework
spring-webmvc
org.springframework
spring-jdbc
org.springframework
spring-core
org.springframework
spring-aop
aspectj
aspectjweaver
1.5.4
aspectj
aspectjrt
1.5.4
org.codehaus.jackson
jackson-mapper-asl
1.9.11
commons-lang
commons-lang
${commons-lang.version}
org.mybatis
mybatis
3.4.2
org.mybatis
mybatis-spring
1.3.1
mysql
mysql-connector-java
5.1.40
c3p0
c3p0
0.9.1.2
jar
org.slf4j
slf4j-log4j12
${slf4j.version}
slf4j-api
org.slf4j
org.springframework
spring-context-support
jar
compile
com.github.pagehelper
pagehelper
4.1.5
org.eclipse.jetty
jetty-maven-plugin
9.2.2.v20140723
org.apache.maven.plugins
maven-compiler-plugin
1.8
UTF-8
web.xml
springdemo
contextConfigLocation
classpath:config/applicationContext.xml
org.springframework.web.context.ContextLoaderListener
springMVC
org.springframework.web.servlet.DispatcherServlet
contextConfigLocation
classpath:config/spring-mvc.xml
2
springMVC
/
login.jsp
encodingFilter
org.springframework.web.filter.CharacterEncodingFilter
encoding
UTF-8
forceEncoding
true
encodingFilter
/
404
/WEB-INF/errors/404.jsp
spring-mvc.xml
jdbc.properties(连接数据库 可以配多个数据源)
jdbc_driverClassName = com.mysql.jdbc.Driver
jdbc_url = jdbc:mysql://localhost:3306/sjk1?useSSL=true&useUnicode=true&characterEncoding=utf-8
jdbc_username = abc
jdbc_password = 123
dw_driverClass = com.mysql.jdbc.Driver
dw_jdbcUrl = jdbc:mysql://localhost:3306/sjk2?useSSL=true&useUnicode=true&characterEncoding=utf-8
dw_username = abc
dw_password = 123
config.xml
applicationContext.xml
DynamicDataSource.java
package com.ssm.test;
import org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource;
public class DynamicDataSource extends AbstractRoutingDataSource {
@Override
protected Object determineCurrentLookupKey() {
// 从自定义的位置获取数据源标识
return DynamicDataSourceHolder.getDataSource();
}
}
DynamicDataSourceHolder.java
package com.ssm.test;
public class DynamicDataSourceHolder {
/**
* 注意:数据源标识保存在线程变量中,避免多线程操作数据源时互相干扰
*/
private static final ThreadLocal THREAD_DATA_SOURCE = new ThreadLocal();
public static String getDataSource() {
return THREAD_DATA_SOURCE.get();
}
public static void setDataSource(String dataSource) {
THREAD_DATA_SOURCE.set(dataSource);
}
public static void clearDataSource() {
THREAD_DATA_SOURCE.remove();
}
}
UserService.java
package com.ssm.service;
import java.util.List;
import com.ssm.model.User;
public interface UserService {
public boolean login(String username,String password) throws Exception;
public List findAll() throws Exception;
}
UserServiceImpl.java
package com.ssm.service.impl;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import com.ssm.dao.IUserDao;
import java.util.List;
import org.springframework.stereotype.Service;
import com.ssm.model.User;
import com.ssm.service.UserService;
@Service("UserService")
public class UserServiceImpl implements UserService{
/**
* 注入UserMapper接口
*/
@Autowired
IUserDao Mapper;
//登录方法的实现,从jsp页面获取username与password
public boolean login(String username, String password) {
User user = Mapper.selectByName(username);
if (user != null) {
if (user.getUsername().equals(username) && user.getPassword().equals(password))
return true;
}
return false;
}
public List findAll() {
// TODO Auto-generated method stub
return Mapper.findAll();
}
}
User.java
package com.ssm.model;
/**
* 描述:登录用户实体类
* 创建人:CNSTT
* 时间:2018年7月18日下午18:06:32
* @version
*/
public class User {
private int id; //主键
private String username; //用户名
private String password; //密码
private int role; //权限
private int roleid; //权限id
private String rolename; //权限英文
private String description; //权限描述
private String task; //工作任务
private String user; //使用人
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public int getRole() {
return role;
}
public void setRole(int role) {
this.role = role;
}
public int getRoleid() {
return roleid;
}
public void setRoleid(int roleid) {
this.roleid = roleid;
}
public String getRolename() {
return rolename;
}
public void setRolename(String rolename) {
this.rolename = rolename;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public String getTask() {
return task;
}
public void setTask(String task) {
this.task = task;
}
public String getUser() {
return user;
}
public void setUser(String user) {
this.user = user;
}
}
IUserDao.java
package com.ssm.dao;
import java.util.List;
import org.apache.ibatis.annotations.Param;
import com.ssm.model.User;
public interface IUserDao {
public User selectById(int id);
public User selectByName(String username);
public List findAll();
}
IUserDao.xml
UserController.java
package com.ssm.controller;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;
import com.ssm.model.User;
import com.ssm.service.UserService;
import com.ssm.test.DynamicDataSourceHolder;
//@Controller注解用于标示本类为web层控制组件
@Controller
//@RequestMapping("/user")用于标定访问时对url位置
@RequestMapping("/user")
//在默认情况下springmvc的实例都是单例模式,所以使用scope域将其注解为每次都创建一个新的实例
@Scope("prototype")
public class UserController {
//自动注入业务层的userService类
@Autowired
UserService userService;
//login业务的访问位置为/user/login
@RequestMapping("/index")
public String login(User user,HttpServletRequest request)throws Exception{
DynamicDataSourceHolder.setDataSource("dataSource");
List users =userService.findAll();//调用findAll方法来获取所有用户list
request.setAttribute("users",users);
return "index";
}
@RequestMapping("/login")
public String helloMvc(){
return "test";
}
}
import javax.servlet.http.HttpServletRequest;
如果报错请按百度操作添加server
正式运行
test.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8" import="java.sql.*,java.io.*,java.util.*,java.text.SimpleDateFormat,java.util.Date"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>
Insert title here
Hello World
index.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8" import="java.sql.*,java.io.*,java.util.*,java.text.SimpleDateFormat,java.util.Date"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>
index
${users.id}
${users.username}
运行启动
jetty:run -Djetty.port=8082