SSM的整合 青旅半醒 2023-10-01 20:11 13阅读 0赞 ### 数据库环境 ### > 创建一个存放书籍数据的数据库表 CREATE DATABASE ssmbuild; USE ssmbuild; CREATE TABLE `books`( `bookID` INT NOT NULL AUTO_INCREMENT COMMENT '书id', `bookName` VARCHAR(100) NOT NULL COMMENT '书名', `bookCounts` INT NOT NULL COMMENT '数量', `detail` VARCHAR(200) NOT NULL COMMENT '描述', KEY `bookID`(`bookID`) )ENGINE=INNODB DEFAULT CHARSET=utf8 INSERT INTO `books`(`bookID`,`bookName`,`bookCounts`,`detail`)VALUES (1,'Java',1,'从入门到放弃'), (2,'MySQL',10,'从删库到跑路'), (3,'Linux',5,'从进门到进牢') ### 基本环境搭建 ### 1. 新建一个Maven项目,ssmbuild,添加web的支持 2. 在pom.xml中**导入相关依赖**和**资源过滤**(还需要手动将jar包添加到lib文件夹中) <dependencies> <!-- junit--> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.13.1</version> <scope>test</scope> </dependency> <!-- 数据库驱动--> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>8.0.15</version> </dependency> <!-- 数据库连接池--> <dependency> <groupId>com.mchange</groupId> <artifactId>c3p0</artifactId> <version>0.9.5.5</version> </dependency> <!-- Servlet jsp--> <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.1</version> </dependency> <dependency> <groupId>javax.servlet.jsp.jstl</groupId> <artifactId>jstl-api</artifactId> <version>1.2</version> </dependency> <dependency> <groupId>taglibs</groupId> <artifactId>standard</artifactId> <version>1.1.2</version> </dependency> <!-- Mybatis--> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.5.7</version> </dependency> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis-spring</artifactId> <version>2.0.6</version> </dependency> <!-- Spring--> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>5.3.15</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> <version>5.3.15</version> </dependency> <!-- 使用Spring的API实现Aop--> <dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjweaver</artifactId> <version>1.9.7</version> </dependency> <!-- lombok--> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <version>1.18.20</version> </dependency> </dependencies> <!--在build中配置resources,来防止我们资源导出失败的问题--> <build> <resources> <resource> <directory>src/main/resources</directory> <includes> <include>**/*.properties</include> <include>**/*.xml</include> </includes> </resource> <resource> <directory>src/main/java</directory> <includes> <include>**/*.properties</include> <include>**/*.xml</include> </includes> <filtering>true</filtering> </resource> </resources> </build> 3. 建立基本结构和配置框架 * com.lzj.dao * com.lzj.entity * com.lzj.service * com.lzj.controller * 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> </configuration> * 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" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> </beans> ### Mybatis层缩写 ### 1. 建立数据库配置文件database.properties jdbc.driver=com.mysql.jdbc.Driver jdbc.url=jdbc:mysql://localhost:3306/ssmbuild?useSSL=false&useUnicode=true&characterEncoding=utf-8&serverTimezone=Asia/Shanghai jdbc.username=root jdbc.password=root #jdbc.url=jdbc:mysql://localhost:3306/mybatis?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone = GMT #如果是将url配置在xml文件中,则按照下面这个复制 #jdbc.url=jdbc:mysql://localhost:3306/mybatis?useSSL=false&useUnicode=true&characterEncoding=utf-8&serverTimezone=Asia/Shanghai 2. IDEA关联数据库 3. 编写MyBatis的核心配置文件mybatis-config.xml <?xml version="1.0" encoding="UTF8" ?> <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd"> <configuration> <!-- 配置数据源,交给Spring去做--> <settings> <!-- 日志--> <setting name="logImpl" value="STDOUT_LOGGING"/> <!-- 开启缓存--> <setting name="cacheEnabled" value="true"/> <!-- <setting name="logImpl" value="LOG4J"/>--> <!-- 开启驼峰映射--> <setting name="mapUnderscoreToCamelCase" value="true"/> </settings> <!-- 别名--> <typeAliases> <package name="com.lzj.entity"/> </typeAliases> <mappers> <mapper class="com.lzj.dao.BookMapper"></mapper> </mappers> </configuration> 4. 编写数据库对应的实体类com.lzj.entity.Books(使用了lombok) @Data @AllArgsConstructor @NoArgsConstructor public class Books implements Serializable { private int bookID; private String bookName; private int bookCounts; private String detail; } 5. 编写Dao层的Mapper接口 @Repository public interface BookMapper { //增加一本书 int addBook(Books books); //删除一本书 int deleteBookById(@Param("bookId") int id); //更新一本书 int updateBook(Books books); //查询一本书 Books queryBookById(@Param("bookId") int id); //查询全部的书 List<Books> queryAllBook(); //通过书名查询书籍 Books queryBookByName(@Param("bookName") String name); } 6. 编写接口对应的Mapper.xml文件,需要导入Mybatis的包 <?xml version="1.0" encoding="UTF8" ?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="com.lzj.dao.BookMapper"> <insert id="addBook" parameterType="Books"> insert into ssmbuild.books (bookName,bookCounts,detail) value (#{bookName},#{bookCounts},#{detail}) </insert> <delete id="deleteBookById" parameterType="int"> delete from ssmbuild.books where bookID=#{bookId} </delete> <update id="updateBook" parameterType="Books"> update ssmbuild.books set bookName=#{bookName},bookCounts=#{bookCounts},detail=#{detail} where bookID=#{bookID} </update> <select id="queryBookById" resultType="Books"> select * from ssmbuild.books where bookID=#{bookId} </select> <select id="queryAllBook" resultType="Books"> select * from ssmbuild.books </select> <select id="queryBookByName" resultType="Books"> select * from ssmbuild.books where bookName=#{bookName} </select> </mapper> 7. 编写Service层的接口和实现类 接口: public interface BookService { //增加一本书 int addBook(Books books); //删除一本书 int deleteBookById(int id); //更新一本书 int updateBook(Books books); //查询一本书 Books queryBookById(int id); //查询全部的书 List<Books> queryAllBook(); //通过书名查询书籍 Books queryBookByName(String name); } 实现类: public class BookServiceImpl implements BookService{ //service调dao层 private BookMapper bookMapper; public void setBookMapper(BookMapper bookMapper) { this.bookMapper = bookMapper; } @Override public int addBook(Books books) { return bookMapper.addBook(books); } @Override public int deleteBookById(int id) { return bookMapper.deleteBookById(id); } @Override public int updateBook(Books books) { return bookMapper.updateBook(books); } @Override public Books queryBookById(int id) { return bookMapper.queryBookById(id); } @Override public List<Books> queryAllBook() { return bookMapper.queryAllBook(); } @Override public Books queryBookByName(String name) { return bookMapper.queryBookByName(name); } } ### Spring层 ### 1. 配置Spring整合MyBatis,这里数据源使用c3p0连接池 2. 编写Spring整合MyBatis的相关配置文件:spring-dao.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"> <!-- <!– 指定要扫描的包,这个包下的注解就会生效–>--> <!-- 由于在末尾指定扫描了dao包,所以可以不需要这个--> <!-- <context:component-scan base-package="com.lzj.dao"></context:component-scan>--> <!-- <context:annotation-config/>--> <!-- 1.关联数据库配置文件--> <context:property-placeholder location="classpath:database.properties"/> <!-- 2.连接池 dbcp:半自动化操作,不能自动连接 c3p0:自动化操作(自动化加载配置文件,并且可以自动设置到对象中) druid hikari 这次导入的是c3p0的连接池 --> <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"> <property name="driverClass" value="${jdbc.driver}"/> <property name="jdbcUrl" value="${jdbc.url}"/> <property name="user" value="${jdbc.username}"/> <property name="password" value="${jdbc.password}"/> <!-- 这个bean中下面这几个可以不需要配置,但还是建议配置上--> <!-- c3p0连接池的私有属性--> <property name="maxPoolSize" value="30"/> <property name="minPoolSize" value="10"/> <!-- 关闭连接后不自动commit--> <property name="autoCommitOnClose" value="false"/> <!-- 获取连接超时时间--> <property name="checkoutTimeout" value="10000"/> <!-- 当获取连接失败重试次数--> <property name="acquireRetryAttempts" value="2"/> </bean> <!-- 3.sqlSessionFactory--> <bean class="org.mybatis.spring.SqlSessionFactoryBean" id="sqlSessionFactory"> <property name="dataSource" ref="dataSource"/> <!-- 绑定Mybatis的配置文件--> <property name="configLocation" value="classpath:mybatis-config.xml"/> </bean> <!-- 配置dao接口扫描包,动态的实现类Dao接口可以注入到Spring容器中--> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <!-- 注入sqlSessionFactory--> <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/> <!-- 要扫描的dao包--> <property name="basePackage" value="com.lzj.dao"/> </bean> <!-- 配置声明式事务--> </beans> 3. Spring整合service层: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" 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"> <!-- <import resource="classpath:spring-dao.xml"/>--> <!-- 1.扫描service下的包--> <context:component-scan base-package="com.lzj.service"/> <!-- 2.将我们的所有业务类,注入到Spring,可以通过配置或者注解实现--> <bean id="BookServiceImpl" class="com.lzj.service.BookServiceImpl"> <!-- 如果这里bookMapper爆红,则需要将spring-dao.xml文件引入,或者通过idea自动组合--> <property name="bookMapper" ref="bookMapper"/> </bean> <!-- 3.声明式事务--> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <!-- 注入数据源--> <property name="dataSource" ref="dataSource"/> </bean> </beans> ### SpringMVC层 ### 1. 编写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"> <!-- DispatchServlet--> <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:applicationContext.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>encodingFilter</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>encodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <!-- Session--> <session-config> <session-timeout>15</session-timeout> </session-config> </web-app> 2. 编写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" xmlns:mvc="http://www.springframework.org/schema/mvc" 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/mvc https://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd"> <!-- 1.注解驱动 mvc--> <mvc:annotation-driven/> <!-- 2.静态资源过滤--> <mvc:default-servlet-handler/> <!-- 3.扫描包:controller--> <context:component-scan base-package="com.lzj.controller"/> <!-- 4.视图解析器--> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <!-- 前缀--> <property name="prefix" value="/WEB-INF/jsp/"/> <!-- 后缀--> <property name="suffix" value=".jsp"/> </bean> </beans> 3. 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" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <import resource="classpath:spring-dao.xml"/> <import resource="classpath:spring-service.xml"/> <import resource="classpath:spring-mvc.xml"/> </beans> ### Controller和视图层编写 ### 1. 编写BookController类 @Controller @RequestMapping("/book") public class BookController { //controller调service层 @Autowired @Qualifier("BookServiceImpl") private BookService bookService; //查询全部的书记并且返回到一个书籍展示页面 @RequestMapping("allBook") public String list(Model model){ List<Books> list = bookService.queryAllBook(); model.addAttribute("list",list); return "allBook"; } //跳转到增加书籍页面 @RequestMapping("/toAddBook") public String toAddPage(){ return "addBook"; } //添加书籍的请求 @RequestMapping("/addBook") public String addBook(Books books){ System.out.println("addBook=>"+books); bookService.addBook(books); return "redirect:/book/allBook"; } //跳转到修改页面 @RequestMapping("/toUpdate/{bookId}") public String toUpdatePage(@PathVariable("bookId") int id,Model model){ Books books = bookService.queryBookById(id); model.addAttribute("QBook",books); return "updateBook"; } //修改书籍的请求 @RequestMapping("/updateBook") public String updateBook(Books books){ System.out.println("updateBook=>"+books); bookService.updateBook(books); return "redirect:/book/allBook"; } //删除书籍的请求 @RequestMapping("/deleteBook/{bookId}") public String deleteBook(@PathVariable("bookId") int id){ bookService.deleteBookById(id); return "redirect:/book/allBook"; } //查询书籍 @RequestMapping("/queryBookByName") public String queryBookByName(String queryBookName,Model model){ Books books = bookService.queryBookByName(queryBookName); List<Books> list = new ArrayList<Books>(); list.add(books); if(books==null){ list=bookService.queryAllBook(); model.addAttribute("error","未查到"); } model.addAttribute("list",list); return "allBook"; } } 2. 编写首页:index.jsp <%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>首页</title> </head> <body> <h3> <a href="${pageContext.request.contextPath}/book/allBook">进入书籍页面</a> </h3> </body> </html> 3. 在/WEB-INF文件夹下创建jsp文件夹 4. 编写书籍列表页面(包括查询全部书籍、查询书名、删除):allBook.jsp <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> <%-- Created by IntelliJ IDEA. User: 86158 Date: 2022/2/23 Time: 20:16 To change this template use File | Settings | File Templates. --%> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>书籍展示</title> <%-- BootStrp美化界面--%> <link href="https://cdn.staticfile.org/twitter-bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"> </head> <body> <div class="container"> <div class="row clearfix"> <div class="col-md-12 column"> <div class="page-header"> <h1> <small>书籍列表————显示所有书籍</small> </h1> </div> </div> <div class="row"> <div class="col-md-4 column"> <a class="btn btn-primary" href="${pageContext.request.contextPath}/book/toAddBook">新增书籍</a> <a class="btn btn-primary" href="${pageContext.request.contextPath}/book/allBook">显示全部书籍</a> </div> <div class="col-md-4 column"></div> <div class="col-md-4 column"> <%-- 查询书籍--%> <form class="form-inline" action="${pageContext.request.contextPath}/book/queryBookByName" method="post" style="float: right"> <span style="color: red;font-weight: bold">${error}</span> <input type="text" name="queryBookName" class="form-control" placeholder="请输入要查询的书籍名称"> <input type="submit" value="查询" class="btn btn-primary"> </form> </div> </div> </div> <div class="row clearfix"> <div class="col-md-12 column"> <table class="table table-hover table-striped"> <thead> <tr> <th>书籍编号</th> <th>书籍名称</th> <th>书籍数量</th> <th>书籍详情</th> <th>操作</th> </tr> </thead> <tbody> <c:forEach var="book" items="${list}"> <tr> <td>${book.bookID}</td> <td>${book.bookName}</td> <td>${book.bookCounts}</td> <td>${book.detail}</td> <td> <a href="${pageContext.request.contextPath}/book/toUpdate/${book.bookID}">修改</a> | <a href="${pageContext.request.contextPath}/book/deleteBook/${book.bookID}">删除</a> </td> </tr> </c:forEach> </tbody> </table> </div> </div> </div> </body> </html> 5. 编写书籍添加页面:addBook.jsp <%-- Created by IntelliJ IDEA. User: 86158 Date: 2022/2/23 Time: 21:00 To change this template use File | Settings | File Templates. --%> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>Title</title> <%-- BootStrp美化界面--%> <link href="https://cdn.staticfile.org/twitter-bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"> </head> <body> <div class="container"> <div class="row clearfix"> <div class="col-md-12 column"> <div class="page-header"> <h1> <small>添加书籍</small> </h1> </div> </div> </div> </div> <form action="${pageContext.request.contextPath}/book/addBook" method="post"> <div class="form-group"> <label>书籍名称</label> <input type="text" name="bookName" class="form-control" required> </div> <div class="form-group"> <label>书籍数量</label> <input type="text" name="bookCounts" class="form-control" required> </div> <div class="form-group"> <label>书籍描述</label> <input type="text" name="detail" class="form-control" required> </div> <div class="form-group"> <input type="submit" class="form-control" value="添加"> </div> </form> </body> </html> 6. 编写书籍修改页面:updateBook.jsp <%-- Created by IntelliJ IDEA. User: 86158 Date: 2022/2/23 Time: 21:42 To change this template use File | Settings | File Templates. --%> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>修改页面</title> <%-- BootStrp美化界面--%> <link href="https://cdn.staticfile.org/twitter-bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"> </head> <body> <div class="container"> <div class="row clearfix"> <div class="col-md-12 column"> <div class="page-header"> <h1> <small>修改书籍</small> </h1> </div> </div> </div> </div> <form action="${pageContext.request.contextPath}/book/updateBook" method="post"> <div class="form-group"> <input type="hidden" name="bookID" class="form-control" value="${QBook.bookID}" > </div> <div class="form-group"> <label>书籍名称</label> <input type="text" name="bookName" class="form-control" value="${QBook.bookName}" required> </div> <div class="form-group"> <label>书籍数量</label> <input type="text" name="bookCounts" class="form-control" value="${QBook.bookCounts}" required> </div> <div class="form-group"> <label>书籍描述</label> <input type="text" name="detail" class="form-control" value="${QBook.detail}" required> </div> <div class="form-group"> <input type="submit" class="form-control" value="修改"> </div> </form> </body> </html> **配置Tomcat,进行运行** ### 项目总体结构 ### ![在这里插入图片描述][watermark_type_d3F5LXplbmhlaQ_shadow_50_text_Q1NETiBAMTEyM-OAgg_size_14_color_FFFFFF_t_70_g_se_x_16_pic_center] ![在这里插入图片描述][watermark_type_d3F5LXplbmhlaQ_shadow_50_text_Q1NETiBAMTEyM-OAgg_size_14_color_FFFFFF_t_70_g_se_x_16_pic_center 1] ### 注意的问题 ### 1. web.xml中导入的xml文件是applicationContext.xml,而不是spring-mvc.xml 2. 需要手动在项目结构中创建lib文件并导入jar包 ![在这里插入图片描述][watermark_type_d3F5LXplbmhlaQ_shadow_50_text_Q1NETiBAMTEyM-OAgg_size_20_color_FFFFFF_t_70_g_se_x_16_pic_center] 3. 如果事务没有自动提交,可在spring-dao.xml中配置声明式事务 <!-- 配置声明式事务--> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <constructor-arg ref="dataSource" /> </bean> <!-- 结合AOP实现事务的置入--> <!-- 配置事务通知--> <tx:advice id="txAdvice" transaction-manager="transactionManager"> <tx:attributes> <tx:method name="add" propagation="REQUIRED"/> <tx:method name="delete" propagation="REQUIRED"/> <tx:method name="update" propagation="REQUIRED"/> <tx:method name="query" read-only="true"/> <tx:method name="*" propagation="REQUIRED"/> </tx:attributes> </tx:advice> <!-- 配置事务切入点--> <aop:config> <aop:pointcut id="txPointCut" expression="execution(* com.lzj.dao.*.*(..))"/> <aop:advisor advice-ref="txAdvice" pointcut-ref="txPointCut"/> </aop:config> 4. spring-dao.xml可以自行更换数据源,以下是Spring的数据源(自行照着修改下) <?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:util="http://www.springframework.org/schema/util" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/util https://www.springframework.org/schema/util/spring-util.xsd"> <!-- DataSource:使用Spring的数据源替换Mybatis的配置 c3p0 dbcp druid 我们这里使用Spring提供的JDBC:org.springframework.jdbc.datasource --> <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <property name="driverClassName" value="com.mysql.jdbc.Driver"/> <property name="url" value="jdbc:mysql://localhost:3306/mybatis?useSSL=false&useUnicode=true&characterEncoding=utf-8&serverTimezone=Asia/Shanghai"/> <property name="username" value="root"/> <property name="password" value="root"/> </bean> <!-- sqlSessionFactory--> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource" /> <!-- 绑定Mybatis配置文件--> <property name="configLocation" value="classpath:mybatis-config.xml"/> <property name="mapperLocations" value="classpath:com/lzj/dao/*.xml"/> </bean> <!-- SqlSessionTemplate:就是我们使用的sqlSession--> <bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate"> <!-- 只能使用构造器注入sqlSessionFactor,因为它没有set方法--> <constructor-arg index="0" ref="sqlSessionFactory"/> </bean> </beans> 5. 乱码问题 mp;serverTimezone=Asia/Shanghai"/> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource" /> <property name="configLocation" value="classpath:mybatis-config.xml"/> <property name="mapperLocations" value="classpath:com/lzj/dao/*.xml"/> </bean> <bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate"> <constructor-arg index="0" ref="sqlSessionFactory"/> </bean> \`\`\` 1. 乱码问题 [watermark_type_d3F5LXplbmhlaQ_shadow_50_text_Q1NETiBAMTEyM-OAgg_size_14_color_FFFFFF_t_70_g_se_x_16_pic_center]: https://img-blog.csdnimg.cn/a1a9b1a9f9614e2ca174d30ee87f3258.png?x-oss-process=image/watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBAMTEyM-OAgg==,size_14,color_FFFFFF,t_70,g_se,x_16#pic_center [watermark_type_d3F5LXplbmhlaQ_shadow_50_text_Q1NETiBAMTEyM-OAgg_size_14_color_FFFFFF_t_70_g_se_x_16_pic_center 1]: https://img-blog.csdnimg.cn/2f5ffa3c1dc743ac925d48cf8a505cea.png?x-oss-process=image/watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBAMTEyM-OAgg==,size_14,color_FFFFFF,t_70,g_se,x_16#pic_center [watermark_type_d3F5LXplbmhlaQ_shadow_50_text_Q1NETiBAMTEyM-OAgg_size_20_color_FFFFFF_t_70_g_se_x_16_pic_center]: https://img-blog.csdnimg.cn/3d88c1f886e64e35958c4c788f1dcbee.png?x-oss-process=image/watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBAMTEyM-OAgg==,size_20,color_FFFFFF,t_70,g_se,x_16#pic_center
相关 SSM的整合 数据库环境 > 创建一个存放书籍数据的数据库表 CREATE DATABASE ssmbuild; USE ssmbuild; CREATE T 青旅半醒/ 2023年10月01日 20:11/ 0 赞/ 14 阅读
相关 整合 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 赞/ 455 阅读
相关 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 赞/ 501 阅读
相关 SSM整合 前提:jdk,maven,tomcat,STS都已经安装配置好了 spring-4.3.7 + mybatis-3.3.0 + maven3.5+jdk8(这个组合会减少很多 悠悠/ 2021年09月28日 07:58/ 0 赞/ 464 阅读
还没有评论,来说两句吧...