一个基本的spring+mybatis所需要的包

spring+mybatis需要的包:
org.springframework.spring-webmvc(spring框架DispatcherServlet需要,spring-webmvc会依赖spring-context,spring-context依赖其它主要spring包)
org.springframework.spring-jdbc(jdbc需要,依赖spring-tx)
org.mybatis.mybatis(mybatis-spring需要)
org.mybatis.mybatis-spring(SqlSessionFactory创建需要,mybatis需要)
mysql.mysql-connector-java(mysql数据库驱动需要)

其它包:
javax.servlet.javax.servlet-api(HttpServletRequest,HttpServletResponse需要)
org.apache.httpcomponents.httpclient(调用其它工程的接口时需要)
log4j.log4j(mybatis需要)
junit.junit(test需要)

druid连接池:需要的包:
com.alibaba.druid(创建数据源需要,druid数据库连接池需要)
可展示Druid的统计信息

c3p0连接池:需要的包
c3p0.c3p0

DBCP连接池:需要的包
commons-dbcp.commons-dbcp

JNDI连接池:需要的包
spring-context

log4j:需要的包
log4j.log4j(mybatis需要)

logback:需要的包
ch.qos.logback.logback-classic(依赖logback-core)

pom.xml

  1 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  2     xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
  3     <modelVersion>4.0.0</modelVersion>
  4     <groupId>com.jmu.ccjoin</groupId>
  5     <artifactId>Spring-mybatis</artifactId>
  6     <packaging>war</packaging>
  7     <version>0.0.1-SNAPSHOT</version>
  8     <name>Spring-mybatis</name>
  9     <url>http://maven.apache.org</url>
 10 
 11     <properties>
 12         <spring.version>4.3.0.RELEASE</spring.version>
 13     </properties>
 14     <dependencies>
 15         <!-- servlet -->
 16         <dependency>
 17             <groupId>javax.servlet</groupId>
 18             <artifactId>javax.servlet-api</artifactId>
 19             <version>4.0.1</version>
 20             <scope>provided</scope>
 21         </dependency>
 22 
 23         <!-- spring-webmvc 依赖了这个包,会自动依赖其它的spring包 -->
 24         <dependency>
 25             <groupId>org.springframework</groupId>
 26             <artifactId>spring-webmvc</artifactId>
 27             <version>${spring.version}</version>
 28         </dependency>
 29 
 30         <dependency>
 31             <groupId>org.springframework</groupId>
 32             <artifactId>spring-jdbc</artifactId>
 33             <version>${spring.version}</version>
 34         </dependency>
 35 
 36         <!-- Mybatis -->
 37         <dependency>
 38             <groupId>org.mybatis</groupId>
 39             <artifactId>mybatis</artifactId>
 40             <version>3.2.0</version>
 41         </dependency>
 42 
 43         <!-- Mybatis+Spring整合 -->
 44         <dependency>
 45             <groupId>org.mybatis</groupId>
 46             <artifactId>mybatis-spring</artifactId>
 47             <version>1.2.0</version>
 48         </dependency>
 49 
 50         <!-- mysql数据库驱动 -->
 51         <dependency>
 52             <groupId>mysql</groupId>
 53             <artifactId>mysql-connector-java</artifactId>
 54             <version>5.1.40</version>
 55         </dependency>
 56 
 57         <!-- druid数据源 -->
 58         <dependency>
 59             <groupId>com.alibaba</groupId>
 60             <artifactId>druid</artifactId>
 61             <version>1.0.13</version>
 62         </dependency>
 63 
 64         <!-- c3p0数据源 -->
 65         <dependency>
 66             <groupId>c3p0</groupId>
 67             <artifactId>c3p0</artifactId>
 68             <version>0.9.1.2</version>
 69         </dependency>
 70 
 71         <!-- DBCP -->
 72         <dependency>
 73             <groupId>commons-dbcp</groupId>
 74             <artifactId>commons-dbcp</artifactId>
 75             <version>1.4</version>
 76         </dependency>
 77 
 78         <!-- log4j -->
 79         <dependency>
 80             <groupId>log4j</groupId>
 81             <artifactId>log4j</artifactId>
 82             <version>1.2.17</version>
 83         </dependency>
 84 
 85         <!-- logback -->
 86         <dependency>
 87             <groupId>ch.qos.logback</groupId>
 88             <artifactId>logback-classic</artifactId>
 89             <version>1.0.6</version>
 90         </dependency>
 91         
 92         <!-- 测试 -->
 93         <dependency>
 94             <groupId>junit</groupId>
 95             <artifactId>junit</artifactId>
 96             <version>3.8.1</version>
 97             <scope>test</scope>
 98         </dependency>
 99         
100         <!-- 支持CGLIB代理 -->
101         <dependency>
102             <groupId>cglib</groupId>
103             <artifactId>cglib</artifactId>
104             <version>3.1</version>
105         </dependency>
106 
107         <!-- httpClient -->
108         <dependency>
109             <groupId>org.apache.httpcomponents</groupId>
110             <artifactId>httpclient</artifactId>
111             <version>4.3.5</version>
112             <scope>compile</scope>
113         </dependency>
114     </dependencies>
115     <build>
116         <finalName>Spring-mybatis</finalName>
117     </build>
118 </project>

web.xml

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 3     xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee"
 4     xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
 5     version="2.5">
 6     <display-name>spring-mybatis</display-name>
 7     <context-param>
 8         <param-name>contextConfigLocation</param-name>
 9         <param-value>classpath:applicationContext.xml</param-value>
10     </context-param>
11     <context-param>
12         <param-name>log4jConfigLocation</param-name>
13         <param-value>classpath:logback.xml</param-value>
14     </context-param>
15     <listener>
16         <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
17     </listener>
18 
19     <!-- <listener>
20         <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
21     </listener> -->
22 
23     <servlet>
24         <servlet-name>servlet</servlet-name>
25         <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
26         <init-param>
27             <param-name>contextConfigLocation</param-name>
28             <param-value>classpath:servlet.xml</param-value>
29         </init-param>
30     </servlet>
31     <servlet-mapping>
32         <servlet-name>servlet</servlet-name>
33         <url-pattern>*.action</url-pattern>
34     </servlet-mapping>
35 
36 </web-app>

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" xmlns:context="http://www.springframework.org/schema/context"
 4     xsi:schemaLocation="http://www.springframework.org/schema/beans
 5            http://www.springframework.org/schema/beans/spring-beans.xsd
 6            http://www.springframework.org/schema/context
 7            http://www.springframework.org/schema/context/spring-context.xsd">
 8 
 9     <!-- 加载配置文件 -->
10     <context:property-placeholder
11         ignore-resource-not-found="true" ignore-unresolvable="true"
12         location="file:/var/opt/spring-mybatis/config.properties" />
13     <context:component-scan
14         base-package="com.jmu.ccjoin.entity,com.jmu.ccjoin.service,com.jmu.ccjoin.core,com.jmu.ccjoin.controller" />
15         
16     <context:annotation-config />
17     <import resource="classpath:applicationContext_datasource.xml"/>
18 </beans>

applicationContext_datasource.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" xmlns:context="http://www.springframework.org/schema/context"
 4     xsi:schemaLocation="http://www.springframework.org/schema/beans
 5            http://www.springframework.org/schema/beans/spring-beans.xsd
 6            http://www.springframework.org/schema/context
 7            http://www.springframework.org/schema/context/spring-context.xsd">
 8 
 9     <!-- 自带的jndi连接池/这里使用了tomcat下的数据源 -->
10     <bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
11         <property name="jndiName" value="java:comp/env/ccjoinDataSource" />
12     </bean>
13 
14     <!-- 配置Druid数据源 -->
15     <!-- <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" 
16         init-method="init" destroy-method="close"> <property name="driverClassName" 
17         value="${jdbc.driverClassName}" /> <property name="url" value="${jdbc.url}" 
18         /> <property name="username" value="${jdbc.username}" /> <property name="password" 
19         value="${jdbc.password}" /> <property name="maxActive" value="${jdbc.maxPoolSize}" 
20         /> <property name="initialSize" value="${jdbc.initialPoolSize}" /> <property 
21         name="maxWait" value="${jdbc.maxIdleTime}" /> <property name="filters" value="stat,log4j" 
22         /> </bean> -->
23 
24     <!-- c3p0连接池 -->
25     <!-- <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" 
26         destroy-method="close"> <property name="driverClass" value="${jdbc.driverClassName}"></property> 
27         <property name="jdbcUrl" value="${jdbc.url}"></property> <property name="user" 
28         value="${jdbc.username}"></property> <property name="password" value="${jdbc.password}"></property> 
29         <property name="minPoolSize" value="${jdbc.minPoolSize}" /> <property name="maxPoolSize" 
30         value="${jdbc.maxPoolSize}" /> <property name="initialPoolSize" value="${jdbc.initialPoolSize}" 
31         /> <property name="maxIdleTime" value="${jdbc.maxIdleTime}" /> </bean> -->
32 
33     <!-- DBCP连接池 -->
34     <!-- <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" 
35         destroy-method="close"> <property name="driverClassName" value="${jdbc.driverClassName}" 
36         /> <property name="url" value="${jdbc.url}" /> <property name="username" 
37         value="${jdbc.username}" /> <property name="password" value="${jdbc.password}" 
38         /> <property name="initialSize" value="${jdbc.initialPoolSize}" /> <property 
39         name="maxIdle" value="${jdbc.maxPoolSize}" /> <property name="minIdle" value="${jdbc.minPoolSize}" 
40         /> <property name="maxActive" value="${jdbc.maxIdleTime}" /> </bean> -->
41 
42     <!-- MyBatis SqlSessionFactoryBean 配置 -->
43     <!-- <bean id="sqlSessionFactory" class="com.baomidou.mybatisplus.spring.MybatisSqlSessionFactoryBean"> 
44         <property name="dataSource" ref="dataSource" /> <property name="configLocation" 
45         value="classpath:mybatis/mybatis-config.xml" /> <property name="typeAliasesPackage" 
46         value="com.jmu.ccjoin.entity" /> <property name="mapperLocations" value="classpath:mybatis/mapper/*Mapper.xml" 
47         /> </bean> -->
48 
49     <!-- <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> 
50         <property name="dataSource"> <ref bean="dataSource" /> </property> <property 
51         name="configLocation" value="classpath:mybatis/mybatis-config.xml"></property> 
52         <property name="typeAliasesPackage" value="com.jmu.ccjoin.entity" /> <property 
53         name="mapperLocations" value="classpath:mybatis/mapper/*Mapper.xml" /> </bean> -->
54     <!-- mybatis文件 -->
55     <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
56         <property name="dataSource" ref="dataSource"></property>
57         <!-- <property name="configLocation" value="classpath:mybatis/mybatis-config.xml"></property> -->
58         <!-- 批量定义别名 ,指定包名,自动扫描包中的类,别名即为类名,首字母大小写无所谓 -->
59         <!-- 自动扫描entity目录, 省掉Configuration.xml里的手工配置 -->
60         <property name="typeAliasesPackage" value="com.jmu.ccjoin.entity"></property>
61         <property name="mapperLocations" value="classpath:mybatis/mapper/*Mapper.xml"></property>
62     </bean>
63 
64     <!-- 配置事务 -->
65     <bean id="transactionManager"
66         class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
67         <property name="dataSource" ref="dataSource" />
68     </bean>
69 
70 
71     <bean id="mapperScannerConfigurer" class="org.mybatis.spring.mapper.MapperScannerConfigurer">
72         <property name="basePackage" value="com.jmu.ccjoin.dao"></property>
73         <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>
74     </bean>
75 </beans>           

file:/var/opt/spring-mybatis/config.properties

1 jdbc.driverClassName=com.mysql.jdbc.Driver
2 jdbc.url=jdbc:mysql://172.16.1.23:3306/jmuv3?useUnicode=true&characterEncoding=utf8
3 jdbc.username=root
4 jdbc.password=123.com
5 jdbc.minPoolSize=5
6 jdbc.maxPoolSize=30
7 jdbc.initialPoolSize=10
8 jdbc.maxIdleTime=1800

servlet.xml

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <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"
 3     xmlns:mvc="http://www.springframework.org/schema/mvc"
 4     xsi:schemaLocation="
 5      http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
 6      http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
 7      http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">
 8 
 9     <context:component-scan
10         base-package="com.jmu.ccjoin.controller" />
11 
12     <!-- 避免IE执行AJAX时,返回JSON出现下载文件 -->
13     <!-- <bean id="mappingJacksonHttpMessageConverter" class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter" /> -->
14 
15     <!-- <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
16         <property name="messageConverters">
17             <list>
18                 <ref bean="mappingJacksonHttpMessageConverter" />json转换器
19             </list>
20         </property>
21     </bean> -->
22 
23     <!-- <bean id="velocityConfigurer" class="org.springframework.web.servlet.view.velocity.VelocityConfigurer">
24         <property name="resourceLoaderPath">
25             <value>/WEB-INF/view</value>
26         </property>
27         <property name="velocityProperties">
28             <props>
29                 <prop key="input.encoding">UTF-8</prop>
30                 <prop key="output.encoding">UTF-8</prop>
31                 <prop key="velocimacro.library">control/macros.vm</prop>
32             </props>
33         </property>
34     </bean> -->
35     
36     <!-- 拦截器 -->
37     <mvc:interceptors>
38         <bean class="com.jmu.ccjoin.interceptor.LogInterceptor" />
39     </mvc:interceptors>
40 </beans>

logback.xml

  1 <?xml version="1.0" encoding="UTF-8" ?>
  2 
  3 <!-- 级别从高到低 OFF 、 FATAL 、 ERROR 、 WARN 、 INFO 、 DEBUG 、 TRACE 、 ALL -->
  4 <!-- 日志输出规则 根据当前ROOT 级别,日志输出时,级别高于root默认的级别时 会输出 -->
  5 <!-- 以下 每个配置的 filter 是过滤掉输出文件里面,会出现高级别文件,依然出现低级别的日志信息,通过filter 过滤只记录本级别的日志 -->
  6 <!-- scan 当此属性设置为true时,配置文件如果发生改变,将会被重新加载,默认值为true。 -->
  7 <!-- scanPeriod 设置监测配置文件是否有修改的时间间隔,如果没有给出时间单位,默认单位是毫秒。当scan为true时,此属性生效。默认的时间间隔为1分钟。 -->
  8 <!-- debug 当此属性设置为true时,将打印出logback内部日志信息,实时查看logback运行状态。默认值为false。 -->
  9 <configuration scan="true" scanPeriod="60 seconds" debug="false">
 10 
 11     <!-- 动态日志级别 -->
 12     <jmxConfigurator/>
 13 
 14     <!-- 定义日志文件 输出位置 -->
 15 <!--     <property name="log_dir" value="logs"/> -->
 16 
 17     <!-- 日志最大的历史 30天 -->
 18 <!--     <property name="maxHistory" value="30"/> -->
 19 
 20     <!-- ConsoleAppender 控制台输出日志 -->
 21     <appender name="console" class="ch.qos.logback.core.ConsoleAppender">
 22         <encoder>
 23             <pattern>
 24                 <!-- 设置日志输出格式 -->
 25                 %d{yyyy-MM-dd HH:mm:ss.SSS} [%-5level] %logger - %msg%n
 26             </pattern>
 27         </encoder>
 28     </appender>
 29 
 30     <!-- ERROR级别日志 -->
 31     <!-- 滚动记录文件,先将日志记录到指定文件,当符合某个条件时,将日志记录到其他文件 RollingFileAppender -->
 32     <appender name="ERROR" class="ch.qos.logback.core.rolling.RollingFileAppender">
 33         <!-- 过滤器,只记录WARN级别的日志 -->
 34         <!-- 果日志级别等于配置级别,过滤器会根据onMath 和 onMismatch接收或拒绝日志。 -->
 35         <filter class="ch.qos.logback.classic.filter.LevelFilter">
 36             <!-- 设置过滤级别 -->
 37             <level>ERROR</level>
 38             <!-- 用于配置符合过滤条件的操作 -->
 39             <onMatch>ACCEPT</onMatch>
 40             <!-- 用于配置不符合过滤条件的操作 -->
 41             <onMismatch>DENY</onMismatch>
 42         </filter>
 43         <!-- 最常用的滚动策略,它根据时间来制定滚动策略.既负责滚动也负责出发滚动 -->
 44         <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
 45             <!--日志输出位置 可相对、和绝对路径 -->
 46             <fileNamePattern>
 47                 ${log.home_url}/error/%d{yyyy-MM-dd}/shop-admin.log
 48             </fileNamePattern>
 49             <!-- 可选节点,控制保留的归档文件的最大数量,超出数量就删除旧文件假设设置每个月滚动,且<maxHistory>是6, 则只保存最近6个月的文件,删除之前的旧文件。注意,删除旧文件是,那些为了归档而创建的目录也会被删除 -->
 50             <maxHistory>${log.maxHistory}</maxHistory>
 51         </rollingPolicy>
 52         <encoder>
 53             <pattern>
 54                 <!-- 设置日志输出格式 -->
 55                 %d{yyyy-MM-dd HH:mm:ss.SSS} [%-5level] %logger - %msg%n
 56             </pattern>
 57         </encoder>
 58     </appender>
 59 
 60 
 61     <!-- WARN级别日志 appender -->
 62     <appender name="WARN" class="ch.qos.logback.core.rolling.RollingFileAppender">
 63         <!-- 过滤器,只记录WARN级别的日志 -->
 64         <!-- 果日志级别等于配置级别,过滤器会根据onMath 和 onMismatch接收或拒绝日志。 -->
 65         <filter class="ch.qos.logback.classic.filter.LevelFilter">
 66             <!-- 设置过滤级别 -->
 67             <level>WARN</level>
 68             <!-- 用于配置符合过滤条件的操作 -->
 69             <onMatch>ACCEPT</onMatch>
 70             <!-- 用于配置不符合过滤条件的操作 -->
 71             <onMismatch>DENY</onMismatch>
 72         </filter>
 73         <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
 74             <!--日志输出位置 可相对、和绝对路径 -->
 75             <fileNamePattern>${log.home_url}/warn/%d{yyyy-MM-dd}/shop-admin.log</fileNamePattern>
 76             <maxHistory>${log.maxHistory}</maxHistory>
 77         </rollingPolicy>
 78         <encoder>
 79             <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%-5level] %logger - %msg%n</pattern>
 80         </encoder>
 81     </appender>
 82 
 83 
 84     <!-- INFO级别日志 appender -->
 85     <appender name="INFO" class="ch.qos.logback.core.rolling.RollingFileAppender">
 86         <filter class="ch.qos.logback.classic.filter.LevelFilter">
 87             <level>INFO</level>
 88             <onMatch>ACCEPT</onMatch>
 89             <onMismatch>DENY</onMismatch>
 90         </filter>
 91         <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
 92             <fileNamePattern>${log.home_url}/info/%d{yyyy-MM-dd}/shop-admin.log</fileNamePattern>
 93             <maxHistory>${log.maxHistory}</maxHistory>
 94         </rollingPolicy>
 95         <encoder>
 96             <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%-5level] %logger - %msg%n</pattern>
 97         </encoder>
 98     </appender>
 99 
100 
101     <!-- DEBUG级别日志 appender -->
102     <appender name="DEBUG" class="ch.qos.logback.core.rolling.RollingFileAppender">
103         <filter class="ch.qos.logback.classic.filter.LevelFilter">
104             <level>DEBUG</level>
105             <onMatch>ACCEPT</onMatch>
106             <onMismatch>DENY</onMismatch>
107         </filter>
108         <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
109             <fileNamePattern>${log.home_url}/debug/%d{yyyy-MM-dd}/shop-admin.log</fileNamePattern>
110             <maxHistory>${log.maxHistory}</maxHistory>
111         </rollingPolicy>
112         <encoder>
113             <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%-5level] %logger - %msg%n</pattern>
114         </encoder>
115     </appender>
116 
117 
118     <!-- TRACE级别日志 appender -->
119     <appender name="TRACE" class="ch.qos.logback.core.rolling.RollingFileAppender">
120         <filter class="ch.qos.logback.classic.filter.LevelFilter">
121             <level>TRACE</level>
122             <onMatch>ACCEPT</onMatch>
123             <onMismatch>DENY</onMismatch>
124         </filter>
125         <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
126             <fileNamePattern>${log.home_url}/trace/%d{yyyy-MM-dd}/shop-admin.log</fileNamePattern>
127             <maxHistory>${log.maxHistory}</maxHistory>
128         </rollingPolicy>
129         <encoder>
130             <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%-5level] %logger - %msg%n</pattern>
131         </encoder>
132     </appender>
133 
134 
135     <!-- root级别   DEBUG -->
136     <root>
137         <!-- 打印debug级别日志及以上级别日志 -->
138         <level value="debug"/>
139         <!-- 控制台输出 -->
140         <appender-ref ref="console"/>
141         <!-- 文件输出 -->
142         <appender-ref ref="ERROR"/>
143         <appender-ref ref="INFO"/>
144         <appender-ref ref="WARN"/>
145         <appender-ref ref="DEBUG"/>
146         <appender-ref ref="TRACE"/>
147     </root>
148 </configuration>

 

posted @ 2018-08-02 09:58  乌瑟尔  阅读(2227)  评论(0编辑  收藏  举报