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.0modelVersion>
  6 
  7     <groupId>org.examplegroupId>
  8     <artifactId>ssmbuildartifactId>
  9     <version>1.0-SNAPSHOTversion>
 10     
 11     <dependencies>
 12         
 13         <dependency>
 14             <groupId>junitgroupId>
 15             <artifactId>junitartifactId>
 16             <version>4.12version>
 17         dependency>
 18         
 19         <dependency>
 20             <groupId>mysqlgroupId>
 21             <artifactId>mysql-connector-javaartifactId>
 22             <version>8.0.16version>
 23         dependency>
 24         
 25         <dependency>
 26             <groupId>com.mchangegroupId>
 27             <artifactId>c3p0artifactId>
 28             <version>0.9.5.2version>
 29         dependency>
 30         
 31         <dependency>
 32             <groupId>javax.servletgroupId>
 33             <artifactId>servlet-apiartifactId>
 34             <version>2.5version>
 35         dependency>
 36         <dependency>
 37             <groupId>javax.servlet.jspgroupId>
 38             <artifactId>jsp-apiartifactId>
 39             <version>2.2version>
 40         dependency>
 41         <dependency>
 42             <groupId>javax.servletgroupId>
 43             <artifactId>jstlartifactId>
 44             <version>1.2version>
 45         dependency>
 46         
 47         <dependency>
 48             <groupId>org.mybatisgroupId>
 49             <artifactId>mybatisartifactId>
 50             <version>3.5.2version>
 51         dependency>
 52         <dependency>
 53             <groupId>org.mybatisgroupId>
 54             <artifactId>mybatis-springartifactId>
 55             <version>2.0.2version>
 56         dependency>
 57         
 58         <dependency>
 59             <groupId>org.springframeworkgroupId>
 60             <artifactId>spring-webmvcartifactId>
 61             <version>5.1.20.RELEASEversion>
 62         dependency>
 63         <dependency>
 64             <groupId>org.springframeworkgroupId>
 65             <artifactId>spring-jdbcartifactId>
 66             <version>5.1.20.RELEASEversion>
 67         dependency>
 68         <dependency>
 69             <groupId>org.projectlombokgroupId>
 70             <artifactId>lombokartifactId>
 71             <version>1.18.20version>
 72         dependency>
 73         <dependency>
 74             <groupId>org.jetbrainsgroupId>
 75             <artifactId>annotationsartifactId>
 76             <version>RELEASEversion>
 77             <scope>compilescope>
 78         dependency>
 79 
 80     dependencies>
 81 
 82     
 83     <build>
 84         <resources>
 85             <resource>
 86                 <directory>src/main/javadirectory>
 87                 <includes>
 88                     <include>**/*.propertiesinclude>
 89                     <include>**/*.xmlinclude>
 90                 includes>
 91                 <filtering>falsefiltering>
 92             resource>
 93             <resource>
 94                 <directory>src/main/resourcesdirectory>
 95                 <includes>
 96                     <include>**/*.propertiesinclude>
 97                     <include>**/*.xmlinclude>
 98                 includes>
 99                 <filtering>falsefiltering>
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 
 7 <configuration>
 8     <typeAliases>
 9         <package name="com.jojo.pojo"/>
10     typeAliases>
11 
12     
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 
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     
10     <context:property-placeholder location="classpath:database.properties"/>
11     
15     
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         
28         <property name="maxPoolSize" value="30"/>
29         <property name="minPoolSize" value="10"/>
30         
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     
40     <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
41         <property name="dataSource" ref="dataSource"/>
42         
43         <property name="configLocation" value="classpath:mybatis-config.xml"/>
44     bean>
45     
46     <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
47         
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     
10     <context:component-scan base-package="com.jojo.service"/>
11     
12     <bean id="BookServiceImpl" class="com.jojo.service.BookServiceImpl">
13         <property name="bookMapper" ref="bookMapper"/>
14     bean>
15     
16     <bean id="dataSourceTransactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
17         
18         <property name="dataSource" ref="dataSource"/>
19     bean>
20     
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     
 8     <servlet>
 9         <servlet-name>springmvcservlet-name>
10         <servlet-class>org.springframework.web.servlet.DispatcherServletservlet-class>
11         <init-param>
12             <param-name>contextConfigLocationparam-name>
13             <param-value>classpath:applicationContext.xmlparam-value>
14         init-param>
15         <load-on-startup>1load-on-startup>
16     servlet>
17     <servlet-mapping>
18         <servlet-name>springmvcservlet-name>
19         <url-pattern>/url-pattern>
20     servlet-mapping>
21 
22     
23     <filter>
24         <filter-name>encodingFilterfilter-name>
25         <filter-class>org.springframework.web.filter.CharacterEncodingFilterfilter-class>
26         <init-param>
27             <param-name>encodingparam-name>
28             <param-value>utf-8param-value>
29         init-param>
30     filter>
31     <filter-mapping>
32         <filter-name>encodingFilterfilter-name>
33         <url-pattern>/*url-pattern>
34     filter-mapping>
35 
36     
37     <session-config>
38         <session-timeout>15session-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     
13     <mvc:default-servlet-handler/>
14     
15     <mvc:annotation-driven/>
16     
17     <context:component-scan base-package="com.jojo.controller"/>
18     
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>