SSM整合
将Spring和myBatis进行整合,从而使用Spring依赖注入以减少代码的耦合,使用SpringMVC处理请求并作出响应。使用myBatis更加简捷地完成数据库操作。
第一步:使用Maven搭建项目
第二步:在pom.xml中导入SSM需要使用到的依赖包
<!--配置junit依赖 -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<!--配置servlet依赖 -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>4.0.0</version>
<scope>provided</scope>
</dependency>
<!-- 引入Spring框架依赖 -->
<!--引入spring-webmvc依赖-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.0.8.RELEASE</version>
</dependency>
<!--引入Spring-jdbc依赖-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.0.8.RELEASE</version>
</dependency>
<!-- 配置数据库依赖 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.38</version>
</dependency>
<!-- 引入数据库连接技术C3P0 -->
<dependency>
<groupId>com.mchange</groupId>
<artifactId>c3p0</artifactId>
<version>0.9.5.2</version>
</dependency>
<!-- 配置mybatis依赖 -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.4.6</version>
</dependency>
<!--MyBatis和Spring整合-->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>1.3.1</version>
</dependency>
<!-- 引入JSTL标签类依赖 -->
<dependency>
<groupId>jstl</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
加载XML代码:
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.xml</include>
</includes>
</resource>
</resources>
第三步:在项目中创建包
com.liuYongQi.SSM.controller com.liuYongQi.SSM.dao com.liuYongQi.SSM.mapper com.liuYongQi.SSM.pojo com.liuYongQi.SSM.util com.liuYongQi.SSM.service com.liuYongQi.SSM. service .impl |
第四步:在resources中创建db.properties
第五步:在resources中创建spring.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.3.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.3.xsd">
<!--使用注解开发-->
<mvc:annotation-driven />
<!--自动扫描:扫描com.zking包下所有类中的注解-->
<context:component-scan base-package="com.liuYongQi.SSM"></context:component-scan>
<!--引入db.properties-->
<context:property-placeholder location="classpath:db.properties"></context:property-placeholder>
<!--配置数据源:C3P0-->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<!--驱动类名 -->
<property name="driverClass" value="${driver_class}"></property>
<!-- url -->
<property name="jdbcUrl" value="${url}"></property>
<!-- 用户名 -->
<property name="user" value="${uname}"></property>
<!-- 密码 -->
<property name="password" value="${upassword}"></property>
<!-- 初始连接池大小 -->
<property name="initialPoolSize" value="${initPoolSize}"></property>
<!-- 连接池中连接最大个数 -->
<property name="maxPoolSize" value="${maxPoolSizes}"></property>
</bean>
<!--配置SqlSessionFactory-->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!--引入数据源-->
<property name="dataSource" ref="dataSource"></property>
<!--加载所有映射文件-->
<property name="mapperLocations" value="classpath:com/liuYongQi/SSM/mapper/*.xml"></property>
<!--typeAliasesPackage对应实体类所在的包,这个时候会自动取包名作为别名-->
<property name="typeAliasesPackage" value="com.liuYongQi.SSM.pojo"></property>
</bean>
<!--4 自动扫描对象关系映射 MapperScannerConfigurer-->
<bean id="scannerConfigurer" class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<!--<property name="sqlSessionFactory" ref="sqlSessionFactory"></property>-->
<property name="basePackage" value="com.liuYongQi.SSM.dao"></property>
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>
</bean>
<!-- 事务管理 -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean>
<!-- 配置视图解析器 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
</beans>
第六步:在com.liuYongQi.SSM.pojo包中创建实体类
package com.liuYongQi.SSM.pojo;
import java.io.Serializable;
/**
* @ClassName: Users
* @Description: 用户表实体类
* @Author: Administrator
* @CreateDate: 2018/10/8 9:26
* @UpdateUser: Administrator
* @UpdateDate: 2018/10/8 9:26
* @UpdateRemark: 修改内容
* @Version: 1.0
*/
public class Users implements Serializable {
private Integer uuid;
private String username;
private String password;
public Users(Integer uuid, String username, String password) {
this.uuid = uuid;
this.username = username;
this.password = password;
}
public Users(String username, String password) {
this.username = username;
this.password = password;
}
public Integer getUuid() {
return uuid;
}
public Users() {
}
public void setUuid(Integer uuid) {
this.uuid = uuid;
}
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;
}
@Override
public String toString() {
return "Users{" +
"uuid=" + uuid +
", username='" + username + '\'' +
", password='" + password + '\'' +
'}';
}
}
第七步:在dao包中创建接口,编写方法
package com.liuYongQi.SSM.dao;
import com.liuYongQi.SSM.pojo.Users;
/**
* @ClassName: UsersDao
* @Description: TODO 用户表实体类Dao接口
* @Author: Administrator
* @CreateDate: 2018/10/19 23:03
* @UpdateUser: Administrator
* @UpdateDate: 2018/10/19 23:03
* @UpdateRemark: 修改内容
* @Version: 1.0
*/
public interface UsersDao {
/**
* @Author Administrator
* @Description //TODO 添加一个用户
* @Date 23:04 2018/10/19
* @Param [users]
* @return int
* @exception
*/
public int addUsers(Users users);
}
第八步:在mapper包中创建实体类的Mapper文件:UsersDaoMapper.xml
第九步:在service中创建接口,在impl包中创建接口的实现类
UsersService接口:
package com.liuYongQi.SSM.service;
import com.liuYongQi.SSM.pojo.Users;
/**
* @ClassName: UsersService
* @Description: TODO 用户表实体类Service接口
* @Author: Administrator
* @CreateDate: 2018/10/19 23:07
* @UpdateUser: Administrator
* @UpdateDate: 2018/10/19 23:07
* @UpdateRemark: 修改内容
* @Version: 1.0
*/
public interface UsersService {
/**
* @Author Administrator
* @Description //TODO 添加一个用户
* @Date 23:08 2018/10/19
* @Param [users]
* @return int
* @exception
*/
public int addUsers(Users users);
}
UsersServiceImpl实现类:
第十步:配置web.xml
<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>Archetype Created Web Application</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<!-- 项目上下文中加载spring.xml -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring.xml</param-value>
</context-param>
<!-- Spring配置监听:作用是在web容器自动运行时加载Spring的相关配置文件,完成类的初始化工作 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- SpringMVC配置 -->
<servlet>
<servlet-name>SpringMvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!--springmvc上下文中加载spring-->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>SpringMvc</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
<!-- 编码配置-->
<filter>
<filter-name>encode</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>encode</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
第十一步:编写视图资源login.jsp
<%--
Created by IntelliJ IDEA.
User: Administrator
Date: 2018/10/19
Time: 23:16
To change this template use File | Settings | File Templates.
--%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8" %>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-type" content="text/html; charset=utf-8"/>
<title></title>
</head>
<body>
<form action="/add.do" method="post">
用户名:<input type="text" name="username" /><br/>
密 码:<input type="text" name="password" /><br/>
<input type="submit" value="提交"/>
</form>
</body>
</html>
第十一步:编写Controller文件
package com.liuYongQi.SSM.controller;
import com.liuYongQi.SSM.pojo.Users;
import com.liuYongQi.SSM.service.UsersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import javax.annotation.Resource;
/**
* @ClassName: UsersController
* @Description: TODO 用户处理类
* @Author: Administrator
* @CreateDate: 2018/10/19 23:17
* @UpdateUser: Administrator
* @UpdateDate: 2018/10/19 23:17
* @UpdateRemark: 修改内容
* @Version: 1.0
*/
@Controller
public class UsersController {
@Resource
//@Autowired
private UsersService usersService;
@RequestMapping("/add")
public String addUsers(Users users){
System.out.println("进来了");
int n=usersService.addUsers(users);
if(n>0){
return "success";
}else{
return "login";
}
}
}
第十二步:测试
添加前
添加后
SSM整合就到这里了,请大家放心参考,这是我测试过的东西。
如果大家想浏览我的下一篇文章,请留言
版权声明:此文章属于原创,不准随意转载:https://blog.csdn.net/LYQ2332826438