文章目录






在eclipse,jdk1.8,MySQL5.7的环境下做SSM整合

1.创建数据库

CREATE TABLE `user` (
`id` int(11) DEFAULT NULL,
`username` varchar(60) DEFAULT NULL,
`password` varchar(60) DEFAULT NULL,
`name` varchar(60) DEFAULT NULL,
`birthday` date DEFAULT NULL,
`gender` int(11) DEFAULT NULL,
`created` datetime DEFAULT NULL,
`updated` datetime DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

2.搭建工程

使用Springmvc、spring、mybatis框架整合完成。

2.1需要的jar包


spring(包括springmvc)



mybatis



mybatis-spring整合包



数据库驱动



第三方连接池。



Json依赖包Jackson
使用eclipse工具搭建SSM_SSM整合



2.2整合思路
  • Dao层:

  1. mybatis.xml,空文件即可,但是需要文件头。
  2. applicationContext-dao.xml

a) 数据库连接Druid

b) SqlSessionFactory对象,需要spring和mybatis整合包下的。

c) 配置mapper文件扫描器。Mapper动态代理开发 增强版

  • Service层:

  1. applicationContext-service.xml包扫描器,扫描@service注解的类。
  2. applicationContext-trans.xml配置事务。

  • Controller层:
  1. springMvc.xml
    a) 包扫描器,扫描@Controller注解的类。

b) 配置注解驱动

c) 配置视图解析器


Web.xml文件:

a) 配置spring监听器

b) 配置前端控制器。
2.3创建工程

创建Dynamic Web Project项目

使用eclipse工具搭建SSM_spring_02

2.4加入jar包

加入刚刚图片上的jar包

​smm整合jar包​​,提取码0huv

2.5加入配置文件

创建config资源文件夹,在里面创建mybatis和spring包

2.5.1 mybatis

空文件即可

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<typeAliases>
<package name="com.pojo"/>
</typeAliases>
<!--dataSource交给Spring -->
</configuration>
2.5.2 applicationContext-dao
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.1.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.1.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd ">
<!-- 1配置 读取properties文件 jdbc.properties -->
<context:property-placeholder
location="classpath:jdbc.properties" />
<!--2创建数据源对象 -->
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="${db.driver}"></property>
<property name="url" value="${db.url}"></property>
<property name="username" value="${db.username}"></property>
<property name="password" value="${db.password}"></property>

</bean>
<!--3sqlSessionFactory -->
<bean id="ssf" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="configLocation" value="classpath:mybatis/mybatis.xml"></property>
<property name="dataSource" ref="dataSource"></property>
</bean>

<!--4 mapper -->
<bean id ="mapper" class="org.mybatis.spring.mapper.MapperScannerConfigurer ">
<!-- 设置Mapper扫描包 -->
<property name="basePackage" value="com.mapper" />
<property name="sqlSessionFactoryBeanName" value="ssf"></property>
</bean>

</beans>
2.5.3 jdbc.properties
db.driver=com.mysql.jdbc.Driver
db.url=jdbc:mysql:///crm?characterEncoding=utf8
db.username=root
db.password=root
2.5.4 log4j.properties
# Global logging configuration
log4j.rootLogger=DEBUG, stdout
# Console output...
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%5p [%t] - %m%n
2.5.5 applicationContext-service.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.1.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.1.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd ">
<!--扫描service -->
<context:component-scan base-package="com.service"/>
</beans>
2.5.6 applicationContext-trans.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.1.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.1.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.1.xsd">
<!--注解式事物 -->
<!-- <tx:annotation-driven/> -->
<!--声明式事物 -->
<!--1事物管理器 -->
<bean id="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean>
<!-- 2 配置事务策略 -->
<tx:advice id="txAdvice">
<tx:attributes>
<tx:method name="query*" read-only="true" />
<tx:method name="find*" read-only="true" />
<tx:method name="insert*" propagation="REQUIRED" />
<tx:method name="update*" propagation="REQUIRED" />
<tx:method name="delete*" propagation="REQUIRED" />
</tx:attributes>
</tx:advice>
<!-- 3 织入事务 -->
<aop:config>
<aop:pointcut
expression="execution(* com.service..*(..))" id="myPointcut" />
<aop:advisor advice-ref="txAdvice"
pointcut-ref="myPointcut" />
</aop:config>

</beans>
2.5.7 springMVC.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.1.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.1.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.1.xsd">
<!-- 1包扫描 -->
<context:component-scan base-package="com.controller"/>
<!-- 2两个处理器 -->
<mvc:annotation-driven/>
<!-- 3视图解析器 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/"></property>
<property name="suffix" value=".jsp"></property>

</bean>

</beans>
2.5.8 web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
id="WebApp_ID" version="3.1">
<display-name>ssm1</display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<!--过滤处理中文乱码 -->
<filter>
<filter-name>character</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>utf-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>character</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!-- spring SSH SSM(I) -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring/applicationContext-*.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- Spring mvc -->
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!-- 默认xml /WEB-INF/springmvc-servelt.xml -->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring/springMVC.xml</param-value>
</init-param>
<!--加载顺序 -->
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>*.action</url-pattern>
</servlet-mapping>
</web-app>
2.6加入静态资源

index.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<a href="userlist.action">显示用户数据</a>
</body>
</html>

show.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>

<body>
<table border="1" cellspacing="0" cellpadding="5" width="500px">
<tr>
<td>编号</td>
<td>用户名</td>
<td>名称</td>
<td>性别</td>
</tr>
<c:forEach items="${abc }" var="user">
<tr>
<td>${user.id }</td>
<td>${user.username }</td>
<td>${user.name }</td>
<td><c:if test="${user.gender == 1}">男</c:if>
<c:if test="${user.gender == 0}">女</c:if></td>
</tr>
</c:forEach>
</table>
</body>
</html>

3.实现页面展示

3.1代码实现

UserController.java

package com.controller;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

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 org.springframework.web.bind.annotation.ResponseBody;

import com.pojo.User;
import com.service.UserService;

/**
*/
@Controller
public class UserController {
@Autowired UserService userService;

//@ResponseBody//不作为逻辑视图的拼接名称
@RequestMapping("/userlist.action")
public String getUsers(Model model,HttpServletRequest request,HttpSession session) {
List<User> users = userService.getUsers();
model.addAttribute("abc",users);
session.setAttribute("list",users);
//return users;//jsson
return "show";//逻辑视图

}
}

/*@Controller
public class UserController {

@Autowired UserService userService;

@RequestMapping("/userlist.action")
@ResponseBody //不作为逻辑视图的拼接名称
public List<User> getUsers() {
List<User> users = userService.getUsers();

//return "show"; //逻辑视图
return users; //json
}

}*/
3.2实现DAO开发
3.2.1 pojo

User.java

package com.pojo;

import java.util.Date;

import org.springframework.stereotype.Component;

/**
*/
@Component
public class User {
private Integer id;
private String username;
private String password;
private String name;
private Date birthday;
private Integer gender;
public User() {
super();
}
public User(Integer id, String username, String password, String name, Date birthday, Integer gender) {
super();
this.id = id;
this.username = username;
this.password = password;
this.name = name;
this.birthday = birthday;
this.gender = gender;
}
public Integer getId() {
return id;
}
public void setId(Integer 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 String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Date getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
public Integer getGender() {
return gender;
}
public void setGender(Integer gender) {
this.gender = gender;
}
@Override
public String toString() {
return "User [id=" + id + ", username=" + username + ", password=" + password + ", name=" + name + ", birthday="
+ birthday + ", gender=" + gender + "]";
}


}
3.2.2 Mapper

UserMapper.java

package com.mapper;

import java.util.List;

import com.pojo.User;

/**
*/
public interface UserMapper {
//查询所有的用户信息
List<User> getUsers();
}
3.2.3 UserMapper.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<!-- namespace:命名空间 -->
<mapper namespace="com.mapper.UserMapper">
<select id="getUsers" resultType="User">
select * from user

</select>
</mapper>
4.3实现Service开发
4.3.1 UserService.java
package com.service;

import java.util.List;

import com.pojo.User;

/**
*/
public interface UserService {
List<User> getUsers();
}
4.3.2 UserServiceImpl
package com.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.mapper.UserMapper;
import com.pojo.User;
import com.service.UserService;

/**
*/
@Service
public class UserServiceImpl implements UserService {

@Autowired
private UserMapper userMapper;
@Override
public List<User> getUsers() {
return userMapper.getUsers();
}


}

结果如下:

使用eclipse工具搭建SSM_spring_03

使用eclipse工具搭建SSM_SpringMVC_04