IDEA 整合SSM
程序员文章站
2022-06-25 10:10:25
...
1.项目结构
我这里用的是Maven中的webapp,需要自己完善项目结构。
2.引入依赖
<dependencies>
<!-- 单元测试 -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
</dependency>
<!-- 1.日志 -->
<!-- 实现slf4j接口并整合 -->
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>1.1.1</version>
</dependency>
<!-- 2.数据库 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.37</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>c3p0</groupId>
<artifactId>c3p0</artifactId>
<version>0.9.1.2</version>
</dependency>
<!-- DAO: MyBatis -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.3.0</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>1.2.3</version>
</dependency>
<!-- 3.Servlet web -->
<dependency>
<groupId>taglibs</groupId>
<artifactId>standard</artifactId>
<version>1.1.2</version>
</dependency>
<dependency>
<groupId>jstl</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.5.4</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
</dependency>
<!-- 4.Spring -->
<!-- 1)Spring核心 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>4.1.7.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>4.1.7.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.1.7.RELEASE</version>
</dependency>
<!-- 2)Spring DAO层 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>4.1.7.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>4.1.7.RELEASE</version>
</dependency>
<!-- 3)Spring web -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>4.1.7.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>4.1.7.RELEASE</version>
</dependency>
<!-- 4)Spring test -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>4.1.7.RELEASE</version>
</dependency>
<!-- redis客户端:Jedis -->
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>2.7.3</version>
</dependency>
<dependency>
<groupId>com.dyuproject.protostuff</groupId>
<artifactId>protostuff-core</artifactId>
<version>1.0.8</version>
</dependency>
<dependency>
<groupId>com.dyuproject.protostuff</groupId>
<artifactId>protostuff-runtime</artifactId>
<version>1.0.8</version>
</dependency>
<dependency>
<groupId>commons-collections</groupId>
<artifactId>commons-collections</artifactId>
<version>3.2</version>
</dependency>
<dependency>
<groupId>c3p0</groupId>
<artifactId>c3p0</artifactId>
<version>0.9.1.2</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.8.13</version>
</dependency>
</dependencies>
因为我的Mapper映射文件是写在src路径下的,而IDEA默认是不会扫描里面的xml文件,所以我们需要进行配置。在pom文件的build标签下加入
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.xml</include>
</includes>
</resource>
</resources>
3.web.xml的配置
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
version="3.1">
<display-name>Archetype Created Web Application</display-name>
<!--启动Spring容器-->
<!--通过ContextLoaderListener在web app启动的时候,
获取contextConfigLocation配置文件的文件名applicationContext.xml,
并进行Spring相关初始化工作-->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!--配置SpringMVC的前端控制器-->
<servlet>
<servlet-name>DispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springmvc.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>DispatcherServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<!--字符编码过滤器-->
<filter>
<filter-name>CharacterEncodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<!--下面两个参数都是确保编码为utf-8-->
<init-param>
<param-name>encoding</param-name>
<param-value>utf-8</param-value>
</init-param>
<init-param>
<param-name>forceEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>CharacterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
4.springmvc.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:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.1.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">
<!--扫描Controller层注解-->
<context:component-scan base-package="cn.yf.controller"/>
<!--配置视图解析器-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/jsp/"/>
<property name="suffix" value=".jsp"/>
</bean>
<!--将springmvc不能处理的请求交给tomcat,也就是实现静态资源的放行-->
<mvc:default-servlet-handler/>
<!--该标签使我们能够使用更多的springmvc高级功能-->
<mvc:annotation-driven/>
</beans>
5.数据库相关文件配置
db.properties
mysql.driver = com.mysql.jdbc.Driver
mysql.url = jdbc:mysql://localhost:3306/sms?characterEncoding=UTF-8
mysql.username = root
mysql.password = admin
mybatis-config.xml(只配置了别名)
<?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="cn.yf.pojo"/>
</typeAliases>
</configuration>
6.Spring配置文件(applicationContext.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:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:jdbc="http://www.springframework.org/schema/jdbc"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<!--Spring的配置文件,这里主要配置和业务逻辑相关的-->
<!--扫描Service层注解-->
<context:component-scan base-package="cn.yf.service.impl"/>
<!--加载数据库配置文件-->
<context:property-placeholder location="classpath:db.properties"/>
<!--c3po数据库连接池-->
<bean id="pooledDataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="${mysql.driver}"/>
<property name="jdbcUrl" value="${mysql.url}"/>
<property name="user" value="${mysql.username}"/>
<property name="password" value="${mysql.password}"/>
</bean>
<!--Spring和Mybatis的整合-->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!--配置Mybatis全局配置文件的位置-->
<property name="configLocation" value="classpath:mybatis-config.xml"/>
<!--指定数据源-->
<property name="dataSource" ref="pooledDataSource"/>
</bean>
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer" >
<property name="basePackage" value="cn.yf.mapper"/>
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
</bean>
<!--事务控制器
对mybatis操作数据库事务控制,spring使用jdbc的事务控制类
-->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="pooledDataSource" />
</bean>
<!--通知-->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<!--传播行为-->
<tx:method name="save*" propagation="REQUIRED"/>
<tx:method name="delete*" propagation="REQUIRED"/>
<tx:method name="insert*" propagation="REQUIRED"/>
<tx:method name="update*" propagation="REQUIRED"/>
<tx:method name="find*" propagation="SUPPORTS" read-only="true"/>
<tx:method name="get*" propagation="SUPPORTS" read-only="true"/>
<tx:method name="select*" propagation="REQUIRED"/>
</tx:attributes>
</tx:advice>
<!--AOP-->
<aop:config>
<!--设置切入点-->
<aop:advisor advice-ref="txAdvice" pointcut="execution(* cn.yf.service.impl.*.*(..))"/>
</aop:config>
<aop:aspectj-autoproxy proxy-target-class="true"/>
</beans>
7.后台测试
1.数据库结构
2.对应的pojo
package cn.yf.pojo;
public class Admin {
private int id;
private String name;
private String password;
public Admin() {
}
public Admin(String name, String password) {
this.name = name;
this.password = password;
}
public Admin(int id, String name, String password) {
this.id = id;
this.name = name;
this.password = password;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
@Override
public String toString() {
return "Admin{" +
"id=" + id +
", name='" + name + '\'' +
", password='" + password + '\'' +
'}';
}
}
3.mapper映射文件以及接口
AdminMapper
package cn.yf.mapper;
import cn.yf.pojo.Admin;
import java.util.List;
public interface AdminMapper {
public List<Admin> getAllAdmin();
public Admin getAdminById(int id);
public boolean addAdmin(Admin admin);
public boolean updateAdmin(Admin admin);
public boolean deleteAdminById(int id);
}
AdminMapper.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">
<mapper namespace="cn.yf.mapper.AdminMapper">
<select id="getAllAdmin" resultType="Admin">
SELECT * FROM admin
</select>
<select id="getAdminById" resultType="Admin" parameterType="int">
SELECT * FROM admin WHERE id = #{id}
</select>
<insert id="addAdmin" parameterType="Admin">
INSERT INTO admin(name,password) VALUES (#{name},#{password})
</insert>
<delete id="deleteAdminById" parameterType="int">
DELETE FROM admin where id = #{id}
</delete>
<update id="updateAdmin" parameterType="Admin">
UPDATE admin set name = #{name},password = #{password} where id = #{id}
</update>
</mapper>
4.service层接口以及其实现类
AdminService
package cn.yf.service;
import cn.yf.pojo.Admin;
import java.util.List;
public interface AdminService {
public List<Admin> getAllAdmin();
public Admin getAdminById(int id);
public boolean addAdmin(Admin admin);
public boolean updateAdmin(Admin admin);
public boolean deleteAdminById(int id);
}
AdminServiceImpl
package cn.yf.service.impl;
import cn.yf.mapper.AdminMapper;
import cn.yf.pojo.Admin;
import cn.yf.service.AdminService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class AdminServiceImpl implements AdminService {
@Autowired
AdminMapper adminMapper;
@Override
public List<Admin> getAllAdmin() {
List<Admin> adminList = adminMapper.getAllAdmin();
return adminList;
}
@Override
public Admin getAdminById(int id) {
Admin admin = adminMapper.getAdminById(id);
return admin;
}
@Override
public boolean addAdmin(Admin admin) {
boolean result = adminMapper.addAdmin(admin);
if(result){
return true;
}
return false;
}
@Override
public boolean updateAdmin(Admin admin) {
boolean result = adminMapper.updateAdmin(admin);
if(result){
return true;
}
return false;
}
@Override
public boolean deleteAdminById(int id) {
boolean result = adminMapper.deleteAdminById(id);
if(result){
return true;
}
return false;
}
}
5.测试
package cn.yf.junit;
import cn.yf.pojo.Admin;
import cn.yf.service.impl.AdminServiceImpl;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import javax.annotation.Resource;
import java.util.List;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "classpath:applicationContext.xml" })
public class testSSM{
@Resource
AdminServiceImpl adminServiceImpl;
@Test
public void test1(){
List<Admin> adminList = adminServiceImpl.getAllAdmin();
System.out.println(adminList);
}
@Test
public void test2(){
Admin admin = adminServiceImpl.getAdminById(1);
System.out.println(admin);
}
@Test
public void test3(){
Admin admin = new Admin("入职","加油");
boolean result = adminServiceImpl.addAdmin(admin);
System.out.println(result);
}
@Test
public void test4(){
Admin admin = new Admin(3,"加油","入职");
boolean result = adminServiceImpl.updateAdmin(admin);
System.out.println(result);
}
@Test
public void test5(){
boolean result = adminServiceImpl.deleteAdminById(4);
System.out.println(result);
}
}
运行成功:
8.SpringMVC测试
Controller
package cn.yf.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class HelloSSM {
@RequestMapping("/success")
public String test(){
return "success";
}
}
success.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>成功</title>
</head>
<body>
<h1>成功了!</h1>
</body>
</html>
测试:
我这里没有将前后连接起来,我太懒了,后面我会写项目练习。
上一篇: 3.5 循环(loop)
下一篇: 空字段引发的*