SSM xml基本配置整合
0、pom.xml
1 <?xml version="1.0" encoding="UTF-8"?> 2 <project xmlns="http://maven.apache.org/POM/4.0.0" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 5 <modelVersion>4.0.0</modelVersion> 6 7 <groupId>org.example</groupId> 8 <artifactId>ssmbuild</artifactId> 9 <version>1.0-SNAPSHOT</version> 10 <!-- 依赖:junit,数据库驱动,连接池,servlet,jsp,mybatis,mybatis-spring,spring--> 11 <dependencies> 12 <!-- junit--> 13 <dependency> 14 <groupId>junit</groupId> 15 <artifactId>junit</artifactId> 16 <version>4.12</version> 17 </dependency> 18 <!-- 数据库驱动,注意版本使用--> 19 <dependency> 20 <groupId>mysql</groupId> 21 <artifactId>mysql-connector-java</artifactId> 22 <version>8.0.16</version> 23 </dependency> 24 <!-- 数据库连接池:c3p0:dbcp --> 25 <dependency> 26 <groupId>com.mchange</groupId> 27 <artifactId>c3p0</artifactId> 28 <version>0.9.5.2</version> 29 </dependency> 30 <!-- servlet- jsp --> 31 <dependency> 32 <groupId>javax.servlet</groupId> 33 <artifactId>servlet-api</artifactId> 34 <version>2.5</version> 35 </dependency> 36 <dependency> 37 <groupId>javax.servlet.jsp</groupId> 38 <artifactId>jsp-api</artifactId> 39 <version>2.2</version> 40 </dependency> 41 <dependency> 42 <groupId>javax.servlet</groupId> 43 <artifactId>jstl</artifactId> 44 <version>1.2</version> 45 </dependency> 46 <!--mybatis--> 47 <dependency> 48 <groupId>org.mybatis</groupId> 49 <artifactId>mybatis</artifactId> 50 <version>3.5.2</version> 51 </dependency> 52 <dependency> 53 <groupId>org.mybatis</groupId> 54 <artifactId>mybatis-spring</artifactId> 55 <version>2.0.2</version> 56 </dependency> 57 <!--spring--> 58 <dependency> 59 <groupId>org.springframework</groupId> 60 <artifactId>spring-webmvc</artifactId> 61 <version>5.1.20.RELEASE</version> 62 </dependency> 63 <dependency> 64 <groupId>org.springframework</groupId> 65 <artifactId>spring-jdbc</artifactId> 66 <version>5.1.20.RELEASE</version> 67 </dependency> 68 <dependency> 69 <groupId>org.projectlombok</groupId> 70 <artifactId>lombok</artifactId> 71 <version>1.18.20</version> 72 </dependency> 73 <dependency> 74 <groupId>org.jetbrains</groupId> 75 <artifactId>annotations</artifactId> 76 <version>RELEASE</version> 77 <scope>compile</scope> 78 </dependency> 79 80 </dependencies> 81 82 <!--静态资源导出问题--> 83 <build> 84 <resources> 85 <resource> 86 <directory>src/main/java</directory> 87 <includes> 88 <include>**/*.properties</include> 89 <include>**/*.xml</include> 90 </includes> 91 <filtering>false</filtering> 92 </resource> 93 <resource> 94 <directory>src/main/resources</directory> 95 <includes> 96 <include>**/*.properties</include> 97 <include>**/*.xml</include> 98 </includes> 99 <filtering>false</filtering> 100 </resource> 101 </resources> 102 </build> 103 104 </project>
1、database.properties
1 jdbc.driver=com.mysql.cj.jdbc.Driver #mysql8.0后版本使用,pom文件引用的版本 2 #mysql.jdbc.url=jdbc:mysql://localhost/数据库名称?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull 3 #安全连接useSSL=true 不会乱码useUnicode=true&characterEncoding=utf8 4 #mysql8.0+增加时区配置;serverTimezone=Asia/Shanghai 5 jdbc.url=jdbc:mysql://localhost:3306/ssmbuild?serverTimezone=Asia/Shanghai&useSSL=true&useUnicode=true&characterEncoding=utf8 6 jdbc.username=root 7 jdbc.password=root
2、mybatis-config.xml
1 <?xml version="1.0" encoding="UTF-8" ?> 2 <!DOCTYPE configuration 3 PUBLIC "-//mybatis.org//DTD Config 3.0//EN" 4 "http://mybatis.org/dtd/mybatis-3-config.dtd"> 5 6 <!-- mybatis的全局配置文件 --> 7 <configuration> 8 <typeAliases> 9 <package name="com.jojo.pojo"/> 10 </typeAliases> 11 12 <!-- 导入mapper.xml映射文件--> 13 <mappers> 14 <mapper class="com.jojo.dao.BookMapper"></mapper> 15 </mappers> 16 17 </configuration>
2.1 扩展***mapper.xml 增删改查
1 <?xml version="1.0" encoding="UTF-8" ?> 2 <!DOCTYPE mapper 3 PUBLIC "-//mybatis.org//DTD Config 3.0//EN" 4 "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> 5 <mapper namespace="com.jojo.dao.BookMapper"> 6 <insert id="addBook" parameterType="Books"> 7 insert into ssmbuild.books(bookName, bookCounts, detail) 8 values (#{bookName},#{bookCounts},#{detail}); 9 </insert> 10 11 <delete id="deleteBookById" parameterType="int"> 12 delete from ssmbuild.books where bookID=#{bookId} 13 </delete> 14 15 <update id="updateBook" parameterType="Books"> 16 update ssmbuild.books 17 set bookName = #{bookName},bookCounts=#{bookCounts},detail=#{detail} 18 where bookID=#{bookID}; 19 </update> 20 21 <select id="queryBookById" parameterType="Books" resultType="com.jojo.pojo.Books"> 22 select * from ssmbuild.books 23 where bookID=#{bookId} 24 </select> 25 <!-- 注意别忘了resultType="com.jojo.pojo.Books" --> 26 <select id="queryAllBook" parameterType="Books" resultType="com.jojo.pojo.Books"> 27 select * from ssmbuild.books 28 </select> 29 </mapper>
3、spring.xml
3.1 spring-dao.xml
1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans xmlns="http://www.springframework.org/schema/beans" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xmlns:context="http://www.springframework.org/schema/context" 5 xsi:schemaLocation="http://www.springframework.org/schema/beans 6 http://www.springframework.org/schema/beans/spring-beans.xsd 7 http://www.springframework.org/schema/context 8 https://www.springframework.org/schema/context/spring-context.xsd"> 9 <!--1.关联数据库配置文件--> 10 <context:property-placeholder location="classpath:database.properties"/> 11 <!--<bean id="propertyConfigurer" 12 class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> 13 <property name="location" value="classpath:database.properties"/> 14 </bean>--> 15 <!--2.连接池 16 dbcp;半自动化操作 17 c3p0;自动化操作(自动化加载配置文件,并且可以自动设置到对象中) 18 druid; 19 hikari:springboot 2.X默认 20 --> 21 <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"> 22 <property name="driverClass" value="${jdbc.driver}"/> 23 <property name="jdbcUrl" value="${jdbc.url}"/> 24 <property name="user" value="${jdbc.username}"/> 25 <property name="password" value="${jdbc.password}"/> 26 27 <!--c3p0连接池私有属性--> 28 <property name="maxPoolSize" value="30"/> 29 <property name="minPoolSize" value="10"/> 30 <!--关闭连接后不自动commit--> 31 <property name="autoCommitOnClose" value="false"/> 32 <!--获取连接超时时间--> 33 <property name="checkoutTimeout" value="10000"/> 34 <!--当获取连接失败重试次数--> 35 <property name="acquireRetryAttempts" value="2"/> 36 </bean> 37 38 39 <!--3.sqlSessionfactory--> 40 <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> 41 <property name="dataSource" ref="dataSource"/> 42 <!--绑定mybatis的配置文件--> 43 <property name="configLocation" value="classpath:mybatis-config.xml"/> 44 </bean> 45 <!--4.配置dao接口扫描包,动态的实现了Dao接口可以注入到Spring容器中!--> 46 <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> 47 <!--注入sqlSessionFactory--> 48 <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/> 49 <!--要扫描的包--> 50 <property name="basePackage" value="com.jojo.dao"/> 51 </bean> 52 53 </beans>
3.2 spring-service.xml
1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans xmlns="http://www.springframework.org/schema/beans" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xmlns:context="http://www.springframework.org/schema/context" 5 xsi:schemaLocation="http://www.springframework.org/schema/beans 6 http://www.springframework.org/schema/beans/spring-beans.xsd 7 http://www.springframework.org/schema/context 8 https://www.springframework.org/schema/context/spring-context.xsd"> 9 <!--1.扫描service下的包--> 10 <context:component-scan base-package="com.jojo.service"/> 11 <!--2.将我们的所有业务类,注入到Spring,可以通过配置,或者注解实现--> 12 <bean id="BookServiceImpl" class="com.jojo.service.BookServiceImpl"> 13 <property name="bookMapper" ref="bookMapper"/> 14 </bean> 15 <!--3.声明式事务配置--> 16 <bean id="dataSourceTransactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> 17 <!--注入数据源--> 18 <property name="dataSource" ref="dataSource"/> 19 </bean> 20 <!--4.aop事务支持--> 21 22 </beans>
4、web.xml
1 <?xml version="1.0" encoding="UTF-8"?> 2 <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee 5 http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd" 6 version="4.0"> 7 <!--DispatcherServlet--> 8 <servlet> 9 <servlet-name>springmvc</servlet-name> 10 <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 11 <init-param> 12 <param-name>contextConfigLocation</param-name> 13 <param-value>classpath:applicationContext.xml</param-value> 14 </init-param> 15 <load-on-startup>1</load-on-startup> 16 </servlet> 17 <servlet-mapping> 18 <servlet-name>springmvc</servlet-name> 19 <url-pattern>/</url-pattern> 20 </servlet-mapping> 21 22 <!--乱码过滤--> 23 <filter> 24 <filter-name>encodingFilter</filter-name> 25 <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> 26 <init-param> 27 <param-name>encoding</param-name> 28 <param-value>utf-8</param-value> 29 </init-param> 30 </filter> 31 <filter-mapping> 32 <filter-name>encodingFilter</filter-name> 33 <url-pattern>/*</url-pattern> 34 </filter-mapping> 35 36 <!--session--> 37 <session-config> 38 <session-timeout>15</session-timeout> 39 </session-config> 40 </web-app>
5、spring-mvc.xml
1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans xmlns="http://www.springframework.org/schema/beans" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xmlns:mvc="http://www.springframework.org/schema/mvc" 5 xmlns:context="http://www.springframework.org/schema/context" 6 xsi:schemaLocation="http://www.springframework.org/schema/beans 7 http://www.springframework.org/schema/beans/spring-beans.xsd 8 http://www.springframework.org/schema/mvc 9 https://www.springframework.org/schema/mvc/spring-mvc.xsd 10 http://www.springframework.org/schema/context 11 https://www.springframework.org/schema/context/spring-context.xsd"> 12 <!--1.静态资源过滤--> 13 <mvc:default-servlet-handler/> 14 <!--2.注解驱动--> 15 <mvc:annotation-driven/> 16 <!--3.扫描包:controller--> 17 <context:component-scan base-package="com.jojo.controller"/> 18 <!--4.视图解析器--> 19 <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> 20 <!--前缀--> 21 <property name="prefix" value="/WEB-INF/jsp/"/> 22 <!--后缀--> 23 <property name="suffix" value=".jsp"/> 24 </bean> 25 26 </beans>
6、applicationContext.xml
1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans xmlns="http://www.springframework.org/schema/beans" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xsi:schemaLocation="http://www.springframework.org/schema/beans 5 http://www.springframework.org/schema/beans/spring-beans.xsd"> 6 7 <import resource="classpath:spring-dao.xml"/> 8 <import resource="classpath:spring-service.xml"/> 9 <import resource="classpath:spring-mvc.xml"/> 10 11 </beans>
作者:冢本八云
个性签名:
如果你是鱼,不要迷恋天空。如果你是鸟,不要迷恋海洋。幸福便好,阳光聚散,不应多说。
如果觉得这篇文章对你有小小的帮助的话,记得在右下角点个“推荐”哦,博主在此感谢!(っ•̀ω•́)っ✎⁾⁾

浙公网安备 33010602011771号