SSM整合(xml配置文件形式)
2023-11-29 23:16:27 星期三
1.依赖引入
| <dependencies> |
| |
| <dependency> |
| <groupId>javax.servlet</groupId> |
| <artifactId>javax.servlet-api</artifactId> |
| <version>4.0.1</version> |
| <scope>provided</scope> |
| </dependency> |
| |
| <dependency> |
| <groupId>org.springframework</groupId> |
| <artifactId>spring-context</artifactId> |
| <version>5.3.20</version> |
| </dependency> |
| |
| <dependency> |
| <groupId>org.springframework</groupId> |
| <artifactId>spring-jdbc</artifactId> |
| <version>5.3.20</version> |
| </dependency> |
| |
| <dependency> |
| <groupId>org.springframework</groupId> |
| <artifactId>spring-web</artifactId> |
| <version>5.3.20</version> |
| </dependency> |
| |
| <dependency> |
| <groupId>org.springframework</groupId> |
| <artifactId>spring-webmvc</artifactId> |
| <version>5.3.20</version> |
| </dependency> |
| |
| <dependency> |
| <groupId>org.aspectj</groupId> |
| <artifactId>aspectjweaver</artifactId> |
| <version>1.9.7</version> |
| </dependency> |
| |
| <dependency> |
| <groupId>org.springframework</groupId> |
| <artifactId>spring-tx</artifactId> |
| <version>5.3.20</version> |
| </dependency> |
| |
| <dependency> |
| <groupId>org.mybatis</groupId> |
| <artifactId>mybatis</artifactId> |
| <version>3.5.4</version> |
| </dependency> |
| |
| <dependency> |
| <groupId>org.mybatis</groupId> |
| <artifactId>mybatis-spring</artifactId> |
| <version>1.3.2</version> |
| </dependency> |
| |
| <dependency> |
| <groupId>mysql</groupId> |
| <artifactId>mysql-connector-java</artifactId> |
| <version>8.0.29</version> |
| </dependency> |
| |
| <dependency> |
| <groupId>org.apache.commons</groupId> |
| <artifactId>commons-dbcp2</artifactId> |
| <version>2.11.0</version> |
| </dependency> |
| |
| <dependency> |
| <groupId>org.projectlombok</groupId> |
| <artifactId>lombok</artifactId> |
| <version>1.18.30</version> |
| </dependency> |
| </dependencies> |
2.application.xml核心配置文件
| <beans xmlns="http://www.springframework.org/schema/beans" |
| xmlns:tx="http://www.springframework.org/schema/tx" |
| xmlns:aop="http://www.springframework.org/schema/aop" |
| xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" |
| xsi:schemaLocation="http://www.springframework.org/schema/beans |
| http://www.springframework.org/schema/beans/spring-beans.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"> |
| |
| |
| <bean id="dataSource" class="org.apache.commons.dbcp2.BasicDataSource"> |
| <property name="driverClassName" value="com.mysql.cj.jdbc.Driver"/> |
| <property name="url" value="jdbc:mysql://localhost:3306/qinmanage"/> |
| <property name="username" value="root"/> |
| <property name="password" value="127003"/> |
| </bean> |
| |
| |
| <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> |
| |
| <property name="configLocation" value="classpath:mybatis-config.xml"/> |
| |
| <property name="dataSource" ref="dataSource"/> |
| </bean> |
| |
| |
| <bean id="studentMapper" class="org.mybatis.spring.mapper.MapperFactoryBean"> |
| |
| <property name="mapperInterface" value="com.bboy.mapper.StudentMapper"/> |
| |
| <property name="sqlSessionFactory" ref="sqlSessionFactory"/> |
| </bean> |
| |
| <bean id="studentService" class="com.bboy.service.impl.StudentServiceImpl"> |
| <property name="studentMapper" ref="studentMapper"/> |
| </bean> |
| |
| <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> |
| <property name="dataSource" ref="dataSource"/> |
| </bean> |
| |
| <tx:advice id="txAdvice" transaction-manager="transactionManager"> |
| |
| |
| |
| |
| |
| |
| |
| |
| <tx:attributes> |
| <tx:method name="*" propagation="REQUIRED" isolation="DEFAULT"/> |
| |
| |
| |
| |
| </tx:attributes> |
| </tx:advice> |
| |
| <aop:config> |
| <aop:advisor advice-ref="txAdvice" pointcut="execution(* com.bboy.service.impl.*.*(..))"/> |
| </aop:config> |
| </beans> |
3.spring-mvc.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" |
| xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> |
| |
| |
| |
| |
| <bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"/> |
| |
| |
| |
| |
| <bean class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter"/> |
| |
| |
| |
| |
| <bean name="/query" class="com.bboy.controller.StudentQueryController"> |
| <property name="studentService" ref="studentService"/> |
| </bean> |
| |
| |
| |
| <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> |
| |
| <property name="prefix" value="/WEB-INF/"/> |
| |
| <property name="suffix" value=".jsp"/> |
| </bean> |
| </beans> |
4.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="com.bboy.pojo"/> |
| </typeAliases> |
| <mappers> |
| <mapper resource="mapper/StudentMapper.xml"/> |
| </mappers> |
| </configuration> |
5.controller(StudentQueryController)
| package com.bboy.controller; |
| |
| import com.bboy.pojo.Student; |
| import com.bboy.service.StudentService; |
| import org.springframework.context.annotation.Configuration; |
| import org.springframework.web.servlet.ModelAndView; |
| import org.springframework.web.servlet.mvc.Controller; |
| |
| import javax.servlet.http.HttpServletRequest; |
| import javax.servlet.http.HttpServletResponse; |
| import java.util.List; |
| |
| |
| |
| |
| |
| |
| public class StudentQueryController implements Controller { |
| private StudentService studentService; |
| |
| public void setStudentService(StudentService studentService) { |
| this.studentService = studentService; |
| } |
| |
| @Override |
| public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws Exception { |
| ModelAndView mav = new ModelAndView(); |
| List<Student> students = studentService.listStudents(); |
| mav.addObject("students",students); |
| mav.setViewName("show"); |
| return mav; |
| } |
| } |
6.pojo(Student)
| package com.bboy.pojo; |
| |
| import lombok.Data; |
| |
| @Data |
| |
| |
| |
| |
| |
| public class Student { |
| private int id ; |
| private String name ; |
| private String birth ; |
| private String sex ; |
| private String password ; |
| } |
| |
7.service
service接口
| package com.bboy.service; |
| |
| import com.bboy.pojo.Student; |
| |
| import java.util.List; |
| |
| public interface StudentService { |
| public List<Student> listStudents(); |
| } |
| |
实现类(StudentServiceImpl)
| package com.bboy.service.impl; |
| |
| import com.bboy.mapper.StudentMapper; |
| import com.bboy.pojo.Student; |
| import com.bboy.service.StudentService; |
| |
| import java.util.List; |
| |
| |
| |
| |
| |
| |
| public class StudentServiceImpl implements StudentService { |
| private StudentMapper studentMapper ; |
| |
| public void setStudentMapper(StudentMapper studentMapper) { |
| this.studentMapper = studentMapper; |
| } |
| |
| @Override |
| public List<Student> listStudents() { |
| return studentMapper.listStudents(); |
| } |
| } |
| |
8.mapper
| package com.bboy.mapper; |
| |
| import com.bboy.pojo.Student; |
| |
| import java.util.List; |
| |
| public interface StudentMapper { |
| public List<Student> listStudents(); |
| } |
| |
9.mapper.StudentMapper.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.bboy.mapper.StudentMapper"> |
| <select id="listStudents" resultType="Student"> |
| select * from t_student; |
| </select> |
| </mapper> |
10.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_4_0.xsd" |
| version="4.0"> |
| |
| <listener> |
| <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> |
| </listener> |
| |
| <context-param> |
| <param-name>contextConfigLocation</param-name> |
| <param-value>classpath:applicationContext.xml</param-value> |
| </context-param> |
| |
| <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-mvc.xml</param-value> |
| </init-param> |
| </servlet> |
| <servlet-mapping> |
| <servlet-name>springmvc</servlet-name> |
| |
| |
| |
| <url-pattern>/</url-pattern> |
| </servlet-mapping> |
| </web-app> |


【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!