ssm整合(springmvc、spring、mybatis) 待我称王封你为后i 2024-04-17 21:30 32阅读 0赞 # 一、项目目录结构 # ![在这里插入图片描述][watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L0NoT0xn_size_16_color_FFFFFF_t_70] # 二、导包 # <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <junit.version>4.12</junit.version> <slf4j.version>1.7.25</slf4j.version> <commons-fileupload.version>1.4</commons-fileupload.version> <commons-io.version>2.6</commons-io.version> <javax.servlet.jsp-api.version>2.3.1</javax.servlet.jsp-api.version> <javax.servlet-api.version>3.1.0</javax.servlet-api.version> <mysql.version>5.1.38</mysql.version> <mybatis.version>3.4.6</mybatis.version> <spring.version>5.0.8.RELEASE</spring.version> <aspect.version>1.8.10</aspect.version> <c3p0.version>0.9.5.2</c3p0.version> <mybatis-spring.version>1.3.2</mybatis-spring.version> <spring-webmvc.version>5.1.6.RELEASE</spring-webmvc.version> </properties> <dependencies> <!-- springmvc支持 --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>${spring-webmvc.version}</version> </dependency> <!-- mybatis整合spring支持 --> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis-spring</artifactId> <version>${mybatis-spring.version}</version> </dependency> <!-- c3p0支持 --> <dependency> <groupId>com.mchange</groupId> <artifactId>c3p0</artifactId> <version>${c3p0.version}</version> </dependency> <!-- spring的aop支持 --> <dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjweaver</artifactId> <version>${aspect.version}</version> </dependency> <dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjrt</artifactId> <version>${aspect.version}</version> </dependency> <!-- spring框架支持 --> <!-- spring上下文支持 --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>${spring.version}</version> </dependency> <!-- spring-jdbc支持 --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> <version>${spring.version}</version> </dependency> <!-- spring整合junit测试 --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-test</artifactId> <version>${spring.version}</version> <scope>test</scope> </dependency> <!-- mybatis框架 --> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>${mybatis.version}</version> </dependency> <!-- mysql connector--> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>${mysql.version}</version> </dependency> <!--jsp、servlet支持, provide属性表示只参与测试,不加载导war包中 --> <dependency> <groupId>javax.servlet.jsp</groupId> <artifactId>javax.servlet.jsp-api</artifactId> <version>${javax.servlet.jsp-api.version}</version> <scope>provided</scope> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <version>${javax.servlet-api.version}</version> <scope>provided</scope> </dependency> <!-- file upload --> <dependency> <groupId>commons-fileupload</groupId> <artifactId>commons-fileupload</artifactId> <version>${commons-fileupload.version}</version> </dependency> <dependency> <groupId>commons-io</groupId> <artifactId>commons-io</artifactId> <version>${commons-io.version}</version> </dependency> <!-- log4j --> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-log4j12</artifactId> <version>${slf4j.version}</version> <scope>test</scope> </dependency> <!-- junit测试支持 --> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>${junit.version}</version> <scope>test</scope> </dependency> </dependencies> # 三、domian、service、controller # ## 3.1.domain包 ## ##### 3.1.1.BaseBean ##### package com.blog.domain; public class BaseBean { private Integer uid; private String uname; private Integer age; private String address; public Integer getUid() { return uid; } public void setUid(Integer uid) { this.uid = uid; } public String getUname() { return uname; } public void setUname(String uname) { this.uname = uname; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } public String getAddress() { return address; } public void setAddress(String address) { this.address = address; } @Override public String toString() { return "BaseBean [uid=" + uid + ", uname=" + uname + ", age=" + age + ", address=" + address + "]"; } } ##### 3.1.2.Account ##### package com.blog.domain; public class Account { private Integer id; private String name; private Double money; private Double transferMoney; 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 Double getMoney() { return money; } public void setMoney(Double money) { this.money = money; } public Double getTransferMoney() { return transferMoney; } public void setTransferMoney(Double transferMoney) { this.transferMoney = transferMoney; } } ## 3.2.service包 ## ##### 3.2.1.AccountService接口 ##### package com.blog.service; import com.blog.domain.Account; public interface AccountService { //转账操作:账户a给账户b转账 public void transfer(Account a,Account b); } ##### 3.2.2.AccountServiceImpl实现类 ##### package com.blog.service; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Isolation; import org.springframework.transaction.annotation.Propagation; import org.springframework.transaction.annotation.Transactional; import com.blog.domain.Account; import com.blog.mapper.AccountMapper; @Service @Transactional(isolation=Isolation.DEFAULT,propagation=Propagation.REQUIRED,readOnly=false) public class AccountServiceImpl implements AccountService { @Autowired private AccountMapper accountMapper; @Override public void transfer(Account a, Account b) { //扣钱 accountMapper.reduce(a); //异常 //int x=1/0; //减钱 accountMapper.increase(b); } } ##### 3.2.3.BaseSerive接口 ##### package com.blog.service; import com.blog.domain.BaseBean; public interface BaseService { public BaseBean getOneById(Integer uid); } ##### 3.2.4.BaseServiceImpl实现类 ##### package com.blog.service; import javax.annotation.Resource; import org.springframework.stereotype.Service; import com.blog.domain.BaseBean; import com.blog.mapper.BaseMapper; @Service public class BaseServiceImpl implements BaseService { @Resource(name="baseMapper") private BaseMapper baseMapper; @Override public BaseBean getOneById(Integer uid) { return baseMapper.getOneById(uid); } } ## 3.3.controlller包 ## ##### 3.3.1.BaseController ##### package com.blog.controller; import javax.annotation.Resource; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.ResponseBody; import com.blog.domain.BaseBean; import com.blog.service.BaseService; @Controller @RequestMapping(value="/base") public class BaseController { @Resource(type=BaseService.class) private BaseService baseService; @GetMapping(value="/getOneById") public @ResponseBody String getOneById(@RequestParam Integer uid) { BaseBean bean = baseService.getOneById(uid); System.out.println(bean); return "成功访问";//给前端相应数据“成功访问” } @GetMapping(value="/") public String toNewJsp() { return "new";//会跳转到/webapp/views/new.jsp,前提是new.jsp存在 } } # 三、mybatis相关配置 # ### 1.mybatis配置文件mybatisConfig.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> <!-- 别名设置有两种,一种是一个一个设置,另外一种是设置某个包,默认别名为类名(大小写都可以,建议小写) --> <!-- 第一种设置 <typeAlias type="com.mybatis_demo.domain.User" alias="user"/>--> <!-- 第二种设置,整个包下面的类都进行别名设置,推荐第二种 --> <package name="com.blog.domain"/> </typeAliases> <!-- 引入映射文件 --> <mappers> <!-- <mapper resource="mapper/UserMapper.xml"/> --> <package name="com.blog.mapper"/> </mappers> </configuration> ### 2.mapper接口和mapper.xml映射文件 ### ##### 2.1.BaseMapper 接口 ##### @Repository public interface BaseMapper { public BaseBean getOneById(Integer uid); } ##### 2.2.BaseMapper .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.blog.mapper.BaseMapper"> <select id="getOneById" parameterType="Integer" resultType="BaseBean"> select * from t_user where uid = #{uid} </select> </mapper> ##### 2.3.AccountMapper接口 ##### package com.blog.mapper; import com.blog.domain.Account; public interface AccountMapper { //账户加钱操作 public void increase(Account a); //账户扣钱操作 public void reduce(Account a); } ##### 2.4.AccountMapper.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.blog.mapper.AccountMapper"> <update id="increase" parameterType="Account"> update t_account set money = money+#{transferMoney} where id=#{id} </update> <update id="reduce" parameterType="Account"> update t_account set money = money-#{transferMoney} where id=#{id} </update> </mapper> # 四、spring相关配置applicationContext-core.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 http://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 http://www.springframework.org/schema/aop/spring-aop.xsd"> <!-- 读取配置文件 --> <context:property-placeholder location="classpath:db.properties"/> <!-- dataSource --> <bean name="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"> <property name="driverClass" value="${dataSource.driverClass}"/> <property name="jdbcUrl" value="${dataSource.jdbcUrl}"/> <property name="user" value="${dataSource.user}"/> <property name="password" value="${dataSource.password}"/> </bean> <!-- spring整合mybatis --> <!-- 1.配置SqlSessionFactoryBean --> <bean id="sqlSessionFactoryBean" class="org.mybatis.spring.SqlSessionFactoryBean"> <!-- 注入数据源 --> <property name="dataSource" ref="dataSource"/> <!-- 告诉spring去哪里加载mybatis配置文件 --> <property name="configLocation" value="classpath:mybatisConfig.xml"/> </bean> <!-- UserDao --> <!-- <bean name="userDao" class="com.spring_mybatis_demo.dao.UserDaoImpl"> <property name="sqlSessionFactory" ref="sqlSessionFactoryBean"/> </bean> --> <!-- mapper动态代理开发 <bean id="userMapper" class="org.mybatis.spring.mapper.MapperFactoryBean"> 注入SqlSessionFactory <property name="sqlSessionFactory" ref="sqlSessionFactoryBean"/> 配置接口 <property name="mapperInterface" value="com.spring_mybatis_demo.mapper.UserMapper"/> </bean> --> <!-- mapper扫描开发 --> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value="com.blog.mapper"/> </bean> <!-- 开启组件扫描,默认开启注解 --> <context:component-scan base-package="com.blog.*"/> <!-- 配置事务核心管理器 --> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource"/> </bean> <!-- 开启注解事务 --> <tx:annotation-driven/> </beans> # 五、springmvc相关配置springmvc-servlet.xml # <?xml version="1.0" encoding="UTF-8"?> <!-- mvc 注解 --> <mvc:annotation-driven/> <!--扫描controller层的@Controller--> <context:component-scan base-package="com.blog.controller"/> <!-- 静态资源映射,防止被DispatcherServlet过滤不可访问 --> <mvc:resources location="/views/" mapping="/views/**"/> <!-- 设置主页面:访问/自动跳转到home视图 --> <mvc:view-controller path="/" view-name="index"/> <!-- 页面重定向 --> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/views/" /> <property name="suffix" value=".jsp" /> </bean> <!-- 全局设置编码UTF-8 --> <mvc:annotation-driven enable-matrix-variables="true"> <mvc:message-converters register-defaults="true"> <bean class="org.springframework.http.converter.StringHttpMessageConverter"> <constructor-arg value="UTF-8" /> </bean> </mvc:message-converters> </mvc:annotation-driven> <!-- 文件上传配置 --> <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver" > <property name="maxUploadSize" value="5120000"/> </bean> \# 六、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" version="3.1"> <display-name>blog</display-name> <!-- 项目启动就加载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.xml配置一下静态资源的映射路径,不然静态资源会被过滤,导致无法访问 --> <servlet> <servlet-name>springmvc</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <!-- springmvc的配置文件名不是springmvc-servlet.xml需要配置下面的参数 <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:applicationContext*.xml,/WEB-INF/springmvc-servlet.xml</param-value> </init-param> --> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>springmvc</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> <!-- 编码过滤器 --> <filter> <filter-name>characterEncodingFilter</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>characterEncodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> </web-app> # 七、db.properties(请根据自己的数据库进行修改) # dataSource.driverClass=com.mysql.jdbc.Driver dataSource.jdbcUrl=jdbc:mysql://localhost:3306/mybatis?useUnicode=true&characterEncoding=utf8 dataSource.user=blog dataSource.password=blog # 八、测试 # ### 1.测试BaseService ### @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration("classpath:applicationContext-core.xml") public class TestBaseService { @Resource(type=BaseService.class) private BaseService baseService; @Test public void test1() { BaseBean bean = baseService.getOneById(15); System.out.println(bean); } } ### 2.测试事务管理 ### package com.blog.test; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import com.blog.domain.Account; import com.blog.service.AccountService; @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration("classpath:applicationContext-core.xml") public class TestAccountService { @Autowired private AccountService accountService; @Test public void test1() { Double transferMoney = 1000d; Account a = new Account(); Account b = new Account(); a.setId(1); b.setId(2); a.setTransferMoney(transferMoney); b.setTransferMoney(transferMoney); accountService.transfer(a, b); } } ### 3.postman测试springmvc ### ##### 3.1.页面跳转() ##### ![在这里插入图片描述][watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L0NoT0xn_size_16_color_FFFFFF_t_70 1] ##### 3.2.响应前端数据 ##### ![在这里插入图片描述][watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L0NoT0xn_size_16_color_FFFFFF_t_70 2] [watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L0NoT0xn_size_16_color_FFFFFF_t_70]: https://image.dandelioncloud.cn/pgy_files/images/2024/04/17/e891f20d27a5470f95b5bae7bc2ebaae.png [watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L0NoT0xn_size_16_color_FFFFFF_t_70 1]: https://image.dandelioncloud.cn/pgy_files/images/2024/04/17/282d6cb4a2f0481dbfb1b9161ac1c239.png [watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L0NoT0xn_size_16_color_FFFFFF_t_70 2]: https://image.dandelioncloud.cn/pgy_files/images/2024/04/17/2b1a2de0cf0f4ef4a79843d489f7c288.png
相关 整合 SSM 1.1 相关依赖 <dependency> <groupId>org.springframework</groupId> <art 不念不忘少年蓝@/ 2022年12月05日 05:21/ 0 赞/ 35 阅读
相关 ssm整合 整合:spring4.2.5+mybatis3.2.8+springMVC+maven 环境:Myeclipse2014+mysql5.5.20+tomcat8+jdk1. 柔光的暖阳◎/ 2022年07月13日 11:19/ 0 赞/ 157 阅读
相关 ssm整合 手把手教你整合最优雅SSM框架:SpringMVC + Spring + MyBatis -------------------- > 我们看招聘信息的时候,经常 古城微笑少年丶/ 2022年07月12日 06:27/ 0 赞/ 45 阅读
相关 ssm整合 手把手教你整合最优雅SSM框架:SpringMVC + Spring + MyBatis -------------------- > 我们看招聘信息的时候,经常 墨蓝/ 2022年07月12日 06:27/ 0 赞/ 74 阅读
相关 ssm整合 在mybatis和spring整合后 , 在把springmvc整合进来 在maven里创建web工程 然后进行mybatis和spring的整合步骤(写在其他博客里) 超、凢脫俗/ 2022年02月12日 10:10/ 0 赞/ 445 阅读
相关 ssm整合 一、注入依赖 <?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://mav 快来打我*/ 2022年01月23日 12:57/ 0 赞/ 456 阅读
相关 SSM--SSM整合 一、项目层级结构以及所需JAR包: ![watermark_type_ZmFuZ3poZW5naGVpdGk_shadow_10_text_aHR0cHM6Ly9ibG9 傷城~/ 2022年01月21日 23:37/ 0 赞/ 486 阅读
相关 ssm整合 ssm整合 项目目录 ![1560559-20190805161522126-1893405258.png][] jar ![1560559-2019080 桃扇骨/ 2021年10月24日 02:56/ 0 赞/ 502 阅读
相关 SSM整合 前提:jdk,maven,tomcat,STS都已经安装配置好了 spring-4.3.7 + mybatis-3.3.0 + maven3.5+jdk8(这个组合会减少很多 悠悠/ 2021年09月28日 07:58/ 0 赞/ 464 阅读
相关 SSM整合 SSM:Spring+SpringMVC+MyBatis 1、导包 1)、Spring 【aop核心】 com.springsource.net.sf. 男娘i/ 2021年09月23日 16:18/ 0 赞/ 514 阅读
还没有评论,来说两句吧...