首页 > 其他分享 >ssm的整合:

ssm的整合:

时间:2022-08-22 12:00:44浏览次数:33  
标签:www http spring springframework ssm 整合 org schema

一:导入所需要的依赖与处理静态资源导出问题:

<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.46</version>
</dependency>
<dependency>
<groupId>com.mchange</groupId>
<artifactId>c3p0</artifactId>
<version>0.9.5.4</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
</dependency>
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>jsp-api</artifactId>
<version>2.2</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>2.0.2</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.2</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.1.9.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.1.9.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>5.1.9.RELEASE</version>

</dependency>
<dependency>
<groupId>jstl</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>

<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId>
<version>5.1.9.RELEASE</version>
</dependency>

</dependencies>
<build>
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
<filtering>false</filtering>
</resource>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
<filtering>false</filtering>
</resource>
</resources>
</build>

二:在resource目录下建立mybatis.xml(其实有没有这个无所谓,这个文件的目的只是看起来像整合了mybatis层,其中mybatis.xml中的跟数据库有关的操作都交给spring去接管)

<?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>
<settings>
<setting name="logImpl" value="STDOUT_LOGGING"/>
</settings>
</configuration>

三:在resource目录下建立datebase.properties文件

jdbc.driverClass=com.mysql.jdbc.Driver
jdbc.jdbcurl=jdbc:mysql://localhost:3306/mybatis?useSSL=false&useUnicode=true&characterEncoding=UTF-8
jdbc.user=root
jdbc.password=ztb

四:在resource目录下建立spring-mapper.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:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">
<!-- 读取数据配置属性文件-->
<context:property-placeholder location="classpath:db.properties"></context:property-placeholder>
<!-- 配置数据源-->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">

<property name="driverClass" value="${jdbc.driverClass}"></property>
<property name="jdbcUrl" value="${jdbc.jdbcurl}"></property>
<property name="user" value="${jdbc.user}"></property>
<property name="password" value="${jdbc.password}"></property>
</bean>
<!-- 配置sqlsessionfactorybean-->
<bean class="org.mybatis.spring.SqlSessionFactoryBean">
<!-- 关联数据源-->
<property name="dataSource" ref="dataSource"></property>
<!--绑定mybatis核心配置文件-->
<property name="configLocation" value="classpath:mybatis-config.xml"></property>
<!-- 给实体类起别名-->
<property name="typeAliasesPackage" value="com.ztb.pojo"></property>
</bean>
<!-- 注册mapper.xml文件 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.ztb.mapper"></property>
</bean>
</beans>

五:在resource目录下建立spring-service.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:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd">
<!-- 要有mapper层的文件-->
<import resource="spring-mapper.xml"></import>
<!--添加包扫描-->
<context:component-scan base-package="com.ztb.service"></context:component-scan>

 <!--下面这些是事务操作,但是加上事务会一直报错,不知道是什么原因-->

<!--&lt;!&ndash; 添加事务管理器&ndash;&gt;-->
<!-- <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">-->
<!--&lt;!&ndash; 切记要配置数据源&ndash;&gt;-->
<!-- <property name="dataSource" ref="dataSource"></property>-->
<!-- </bean>-->


<!-- <tx:advice id="myadvice" transaction-manager="transactionManager">-->
<!-- <tx:attributes>-->
<!-- &lt;!&ndash; *指的是为所有方法添加事务 REQUIRED指的是增删改,&ndash;&gt;-->
<!-- <tx:method name="*" propagation="NOT_SUPPORTED"/>-->

<!-- </tx:attributes>-->
<!-- </tx:advice>-->
<!--<aop:config>-->
<!-- <aop:pointcut id="mycut" expression="execution(* com.ztb.service.*.*(..))"/>-->
<!--&lt;!&ndash; 把myadvice的事务绑定到service的所有方法中&ndash;&gt;-->
<!-- <aop:advisor advice-ref="myadvice" pointcut-ref="mycut"></aop:advisor>-->
<!--</aop:config>-->
</beans>

六:在resource目录下建立spring-controller.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: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.xsd
http://www.springframework.org/schema/context
https://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc
https://www.springframework.org/schema/mvc/spring-mvc.xsd">
<!-- 要有上一层servcie的文件-->
<import resource="spring-service.xml"></import>
<!--添加包扫描-->
<context:component-scan base-package="com.ztb.controller"/>
<!-- 添加注解驱动-->
<mvc:annotation-driven></mvc:annotation-driven>
<!-- 如果是ajax请求,那么不需要配置视图解析器-->

<!-- 如果不是ajax请求,则需要配置视图解析器-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" id="internalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/"/>
<property name="suffix" value=".jsp"/>
</bean>
</beans>

上面这几个文件分别对应ssm三层架构的几个接口与类

七:在web.xml文件中注册spring和springmvc框架,以及字符编码过滤器

<?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_4_0.xsd"
version="4.0">
<!--字符编码过滤问题-->
<filter>
<filter-name>character-filter</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</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>

<!-- 注册mvc框架-->
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring-controller.xml</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<!-- 注册spring框架,目的就是启动spring容器-->
<listener>
<listener-class>org.springframework.web.context.ContextCleanupListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring-*.xml</param-value>
</context-param>
</web-app>

八:建立实体类包pojo,包中建立实体类

package com.ztb.pojo;

public class User {
private Integer id;
private String name;
private String pwd;

public User() {
}

public User(Integer id, String name, String pwd) {
this.id = id;
this.name = name;
this.pwd = pwd;
}

public Integer getId() {
return id;
}

public void setId(Integer id) {
this.id = id;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public String getPwd() {
return pwd;
}

public void setPwd(String pwd) {
this.pwd = pwd;
}

@Override
public String toString() {
return "User{" +
"id=" + id +
", name='" + name + '\'' +
", pwd='" + pwd + '\'' +
'}';
}
}

九:建立mapper包,包中建立接口与对应的数据库的操作的xml文件,注意接口与xml文件的名字必须一致

接口:

package com.ztb.mapper;

import com.ztb.pojo.User;

import java.util.List;

public interface UserMapper {
List<User> selectAll();

int deleteid(Integer id);

int insertuser(User user);

int updateuser(User user);
}

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="com.ztb.mapper.UserMapper">
<select id="selectAll" resultType="user">
select * from user
</select>

<delete id="deleteid" parameterType="int">
delete from user where id=#{id}
</delete>

<insert id="insertuser" parameterType="user">
insert into user (id,name,pwd) values (#{id},#{name},#{pwd})
</insert>
<update id="updateuser" parameterType="user">
update user set pwd=#{pwd},name=#{name} where id=#{id}
</update>
</mapper>

十:建立service包,包中建立接口与对应的类

接口:

public interface UserService {
List<User> selectAll();

int deleteid(Integer id);

int insertuser(User user);
int updateuser(User user);
}

实现类:

@Service
public class UserServiceImpl implements UserService {
@Autowired
UserMapper userMapper;
public List<User> selectAll() {
return userMapper.selectAll();
}

public int deleteid(Integer id) {
return userMapper.deleteid(id);
}

public int insertuser(User user) {
return userMapper.insertuser(user);
}

public int updateuser(User user) {
return userMapper.updateuser(user);
}
}

十一:进行测试,测试通过后在进行controller层

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {
"classpath:spring-service.xml"})
public class ServiceTest {
@Autowired
UserService userService;
@Test
public void test1(){
List<User> list = userService.selectAll();
for (User user : list) {
System.out.println(user);
}
}
@Test
public void test2(){
int deleteid = userService.deleteid(16);
System.out.println(deleteid);
}

@Test
public void test3(){
int deleteid = userService.insertuser(new User(14,"宋","www"));
System.out.println(deleteid);
}
}

十二:进行controller层的编写:

@Controller
public class UserController {
@Autowired
UserService userService;
@RequestMapping("showuser")
public String showuser(HttpServletRequest request){
List<User> list = userService.selectAll();
request.setAttribute("userlist",list);
return "main";
}

十三:配置tomcat,测试浏览器与服务器的交互

 

遇到的一些问题以及解决方法:

一:spring单元测试的依赖版本要和spring-*各个依赖的版本相同,否则会报错

二:添加包扫描时,要用<context:compoent-scan>,不能用<context:scan>,否则会报无法找到bean的错。

三:在spring-service.xml文件中一定要导入spring-mapper.xml,在在spring-controller.xml文件中一定要导入spring-service.xml,否则会报错

四:需要在web-inf目录下新建lib包,然后加入全部的外部依赖,步骤file-project settings-artifacts

五:就是事务的切面配置,只要配置事务切面,就会报错,不知道该怎么解决

 

标签:www,http,spring,springframework,ssm,整合,org,schema
From: https://www.cnblogs.com/zhangtaibing/p/16612374.html

相关文章

  • spring 整合 rabbitmq
    一、基本配置1、pom添加以下jar<dependency><groupId>com.fasterxml.jackson.core</groupId><artifactId>jackson-databind</artifactId><version>2.7.5</ve......
  • 基于SpringBoot的SSMP整合
    前言实体类开发————使用Lombok快速制作实体类Mapper开发————整合MyBatisPlus,制作数据层测试Service开发————基于MyBatisPlus进行增量开发,制作业务层测试......
  • SpringBoot整合Redis实现常用功能
    SpringBoot整合Redis实现常用功能建议大小伙们,在写业务的时候,提前画好流程图,思路会清晰很多。文末有解决缓存穿透和击穿的通用工具类。1登陆功能我想,登陆功能......
  • css属性样式整合
    font-size字体大小常用色值:#000黑色;#fff白色;#ccc、#333、#2f2f2f、#666、#ddd灰色;rgb表示法:color:rgb(255,255,255);rgba表示法:color:rgba(255,255,255,0.5)第四位......
  • SpringBoot 整合flyway
    目录【pom.xml】【启动类】【配置属性类】【配置类】【配置文件】【IDEA插件支持】【参考文章】前言:本文章专用于因版本问题导致springboot整合flyway不成功无法自动迁移......
  • 成功进行数据整合的案例
    目前大多数公司使用了许多ERP系统、CRM系统等七八个系统来推动公司的运行,提高公司的效率。不可置疑,这是十分有效的,但是随着各个系统的独立运行,尤其是不同的系统所使用的数......
  • 关于SpringBoot整合redis使用Lettuce客户端超时问题
    问题起因使用到Lettuce连接redis,一段时间后不操作,再去操作redis,会报连接超时错误,在其重连后又可使用。原因是:Lettuce自适应拓扑刷新(Adaptiveupdates)与定时拓扑刷新(Peri......
  • docker compose搭建redis7.0.4高可用一主二从三哨兵集群并整合SpringBoot【图文完整版
    一、前言redis在我们企业级开发中是很常见的,但是单个redis不能保证我们的稳定使用,所以我们要建立一个集群。redis有两种高可用的方案:HighavailabilitywithRedisSen......
  • Linux下搭建ZooKeeper集群并整合Dubbo配置
    1.环境说明Zookeeper不仅可以单机提供服务,同时也支持多机组成集群来提供服务,实际上Zookeeper还支持另外一种伪集群的方式,也就是可以在一台物理机上运行多个Zookeeper......
  • SSM环境搭建
    一、下载安装Spring下载Spring地址:https://repo.spring.io/libs-release-local/沿着org-springframework-spring,可以看到各版本的压缩包下载链接  解压后有如下几个......