Mybatis——SSM 冷不防 2022-05-24 05:42 168阅读 0赞 目录结构 ![70][] 1、相关依赖 <dependencies> <dependency> <groupId>org.springframework</groupId> <artifactId>spring</artifactId> <version>2.5.6</version> </dependency> <!--spring IOC--> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>5.0.2.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>4.3.16.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-beans</artifactId> <version>4.3.14.RELEASE</version> </dependency> <!--jdbc--> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> <version>4.3.16.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-orm</artifactId> <version>4.3.16.RELEASE</version> </dependency> <!--web--> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-web</artifactId> <version>4.3.16.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>4.3.16.RELEASE</version> </dependency> <!--aop--> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-aop</artifactId> <version>4.3.16.RELEASE</version> </dependency> <!--日志--> <dependency> <groupId>commons-logging</groupId> <artifactId>commons-logging</artifactId> <version>1.2</version> </dependency> <!--JSTL--> <dependency> <groupId>jstl</groupId> <artifactId>jstl</artifactId> <version>1.2</version> </dependency> <!--数据库驱动--> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>6.0.6</version> </dependency> <!--mybatis--> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.4.5</version> </dependency> <!--mybatis-spring--> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis-spring</artifactId> <version>1.3.1</version> </dependency> <!--测试--> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.11</version> <scope>test</scope> </dependency> </dependencies> 2、配置mybatis配置文件 mybatis-config.xml、mapper.xml、dbConfig.properties 3、配置spring配置文件 在web.xml中做出spring的配置 <!--Spring配置--> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:applicationContext.xml</param-value> </context-param> 并新建spring配置文件 在web.xml中配置前端控制器 <!--前端控制器--> <!--springMVC配置--> <servlet> <servlet-name>spring</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>spring</param-name> <param-value>/</param-value> </init-param> </servlet> 在WEB-INFO下创建spring-servlet.xml 在其中配置上 <!--自动扫描所有文件--> <!--只扫描控制器--> <context:component-scan base-package="bean" use-default-filters="false"> <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/> </context:component-scan> <!--视图解析器--> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/pages/"></property> <property name="suffix" value=".jsp"></property> </bean> <mvc:annotation-driven></mvc:annotation-driven> <mvc:default-servlet-handler/> 配置applicationContext.xml <!--Spring希望管理所有的业务逻辑组件,等。。。。。--> <context:component-scan base-package="java"> <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/> </context:component-scan> <!--引入数据库配置文件--> <context:property-placeholder location="classpath:dbConfig.properties"/> <!--Spring用来控制业务逻辑。数据源、事物控制、aop--> <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"> <property name="jdbcUrl" value="${jdbc.driver}"/> <property name="driverClass" value="${jdbc.url}"/> <property name="user" value="${jdbc.username}"/> <property name="password" value="${jdbc.password}"/> </bean> <!--spring事物管理器--> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource"></property> </bean> <!--开启基于注解的事物--> <tx:annotation-driven transaction-manager="transactionManager"/> <!-- 整合mybatis 目的:1、spring管理所有组件。mapper的实现类。 service==》dao @Autowired:自动注入 2、spring管理事物,spring声明式事物 --> <!--创建出SqlSessionFactoryBean对象--> <bean id="sqlSessionFactoryBean" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource"/> <!--指定mybatis全局配置文件的位置--> <property name="configLocation" value="classpath:mybatis-config.xml"/> <!--指定mapper配置文件的位置--> <property name="mapperLocations" value="classpath:mapper/*.xml"/> </bean> <!-- 扫描所有的mapper接口,让这些mapper能够自动注入 base-package:指定mapper接口的包名 --> <mybatis-spring:scan base-package="dao"/> 编辑controller层和service层以及页面 @Service public class EmployeeService { @Autowired private EmployeeMapper mapper; public List<Employee> getEmps(){ return mapper.getEmps(); } } @Controller public class EmployeeController { @Autowired EmployeeService employeeService; @RequestMapping(value = "/getEmps") public String emps(Map<String,Object> map){ List<Employee> emps = employeeService.getEmps(); map.put("allEmps",emps); return "list"; } } <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> <html> <head> <title>员工列表</title> </head> <body> <table> <tr> <td>id</td> <td>lastName</td> <td>email</td> <td>gender</td> </tr> <c:forEach items="#{ allEmps}" var="emp"> <tr> <td>#{ emp.id}</td> <td>#{ emp.lastName}</td> <td>#{ emp.email}</td> <td>#{ emp.gender}</td> </tr> </c:forEach> </table> </body> </html> <html> <body> <h2>Hello World!</h2> <a href="getEmps">查询所有员工</a> </body> </html> [70]: /images/20220524/06ed0c912de1431a9bfad5244219ef17.png
还没有评论,来说两句吧...