整个配置在最后,主要记录如何找这些 jar 包 和 如何找这些 jar 包的依赖的包
1.为Maven项目引用 相关jar 包
mybatis jar包和依赖包最容易找
打开 https://mvnrepository.com/ , 在顶部搜索栏搜索 mybatis spring 关键词 结果如下
点击第一个搜索项
我选的 是 1.3.0 这个版本的 点击进去
其他的 就不用多说了 接下来我直接贴出我的 pom.xml 的配置
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.banywl</groupId> 5 <artifactId>xcmp</artifactId> 6 <packaging>war</packaging> 7 <version>1.0-SNAPSHOT</version> 8 <name>xcmp Maven Webapp</name> 9 <url>http://maven.apache.org</url> 10 <properties> 11 <!-- 文件拷贝时的编码 --> 12 <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> 13 <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> 14 <!-- 编译时的编码 --> 15 <maven.compiler.encoding>UTF-8</maven.compiler.encoding> 16 <!-- 版本设置 --> 17 <spring.version>4.2.5.RELEASE</spring.version> 18 <mysql.conn.version>5.1.38</mysql.conn.version> 19 <dbcp.version>1.4</dbcp.version> 20 <mybatis.version>3.4.1</mybatis.version> 21 <cglib.version>3.2.2</cglib.version> 22 <commons-logging.version>1.2</commons-logging.version> 23 <log4j-core.version>2.3</log4j-core.version> 24 <ognl.version>3.1.8</ognl.version> 25 <javassist.version>3.20.0-GA</javassist.version> 26 <log4j.version>1.2.17</log4j.version> 27 <aspectjweaver.version>1.5.3</aspectjweaver.version> 28 <aspectjrt.version>1.5.3</aspectjrt.version> 29 <mybatis-spring.version>1.3.0</mybatis-spring.version> 30 <servlet-api.version>2.5</servlet-api.version> 31 <hibernate-validator.version>5.0.2.Final</hibernate-validator.version> 32 <commons-fileupload.version>1.3.1</commons-fileupload.version> 33 <commons-io.version>2.2</commons-io.version> 34 <jackson-databind.version>2.8.9</jackson-databind.version> 35 </properties> 36 37 <dependencies> 38 <dependency> 39 <groupId>junit</groupId> 40 <artifactId>junit</artifactId> 41 <version>4.12</version> 42 <scope>test</scope> 43 </dependency> 44 <!-- Mysql 驱动 --> 45 <dependency> 46 <groupId>mysql</groupId> 47 <artifactId>mysql-connector-java</artifactId> 48 <version>${mysql.conn.version}</version> 49 </dependency> 50 <!-- DBCP 连接池 --> 51 <dependency> 52 <groupId>commons-dbcp</groupId> 53 <artifactId>commons-dbcp</artifactId> 54 <version>${dbcp.version}</version> 55 </dependency> 56 <!-- MyBatis --> 57 <dependency> 58 <groupId>org.mybatis</groupId> 59 <artifactId>mybatis</artifactId> 60 <version>${mybatis.version}</version> 61 </dependency> 62 <dependency> 63 <groupId>cglib</groupId> 64 <artifactId>cglib</artifactId> 65 <version>${cglib.version}</version> 66 </dependency> 67 <dependency> 68 <groupId>commons-logging</groupId> 69 <artifactId>commons-logging</artifactId> 70 <version>${commons-logging.version}</version> 71 </dependency> 72 <dependency> 73 <groupId>org.apache.logging.log4j</groupId> 74 <artifactId>log4j-core</artifactId> 75 <version>${log4j-core.version}</version> 76 </dependency> 77 <dependency> 78 <groupId>ognl</groupId> 79 <artifactId>ognl</artifactId> 80 <version>${ognl.version}</version> 81 </dependency> 82 <dependency> 83 <groupId>org.javassist</groupId> 84 <artifactId>javassist</artifactId> 85 <version>${javassist.version}</version> 86 </dependency> 87 <!-- 日志依赖 --> 88 <dependency> 89 <groupId>log4j</groupId> 90 <artifactId>log4j</artifactId> 91 <version>${log4j.version}</version> 92 </dependency> 93 <!-- spring --> 94 <dependency> 95 <groupId>org.springframework</groupId> 96 <artifactId>spring-context</artifactId> 97 <version>${spring.version}</version> 98 </dependency> 99 <dependency> 100 <groupId>org.springframework</groupId> 101 <artifactId>spring-jdbc</artifactId> 102 <version>${spring.version}</version> 103 </dependency> 104 <dependency> 105 <groupId>org.springframework</groupId> 106 <artifactId>spring-test</artifactId> 107 <version>${spring.version}</version> 108 <scope>test</scope> 109 </dependency> 110 <dependency> 111 <groupId>aspectj</groupId> 112 <artifactId>aspectjweaver</artifactId> 113 <version>${aspectjweaver.version}</version> 114 </dependency> 115 <dependency> 116 <groupId>aspectj</groupId> 117 <artifactId>aspectjrt</artifactId> 118 <version>${aspectjrt.version}</version> 119 </dependency> 120 <!-- spring MVC --> 121 <dependency> 122 <groupId>org.springframework</groupId> 123 <artifactId>spring-webmvc</artifactId> 124 <version>${spring.version}</version> 125 </dependency> 126 <!-- mybatis和spring整合 --> 127 <dependency> 128 <groupId>org.mybatis</groupId> 129 <artifactId>mybatis-spring</artifactId> 130 <version>${mybatis-spring.version}</version> 131 </dependency> 132 <!-- servlet --> 133 <dependency> 134 <groupId>javax.servlet</groupId> 135 <artifactId>servlet-api</artifactId> 136 <version>${servlet-api.version}</version> 137 <scope>provided</scope> 138 </dependency> 139 <!--hibernate 校验--> 140 <dependency> 141 <groupId>org.hibernate</groupId> 142 <artifactId>hibernate-validator</artifactId> 143 <version>${hibernate-validator.version}</version> 144 </dependency> 145 <!--文件上传--> 146 <dependency> 147 <groupId>commons-fileupload</groupId> 148 <artifactId>commons-fileupload</artifactId> 149 <version>${commons-fileupload.version}</version> 150 </dependency> 151 <dependency> 152 <groupId>commons-io</groupId> 153 <artifactId>commons-io</artifactId> 154 <version>${commons-io.version}</version> 155 </dependency> 156 <!--json 转换--> 157 <dependency> 158 <groupId>com.fasterxml.jackson.core</groupId> 159 <artifactId>jackson-databind</artifactId> 160 <version>${jackson-databind.version}</version> 161 </dependency> 162 </dependencies> 163 <build> 164 <finalName>xcmp</finalName> 165 <resources> 166 <!--编译之后包含xml--> 167 <resource> 168 <directory>src/main/java</directory> 169 <includes> 170 <include>**/*.xml</include> 171 <include>**/*.properties</include> 172 </includes> 173 <filtering>true</filtering> 174 </resource> 175 </resources> 176 <plugins> 177 <plugin> 178 <!--mybatis 逆向工具 Maven 插件--> 179 <groupId>org.mybatis.generator</groupId> 180 <artifactId>mybatis-generator-maven-plugin</artifactId> 181 <version>1.3.5</version> 182 <configuration> 183 <verbose>true</verbose> 184 <overwrite>true</overwrite> 185 </configuration> 186 </plugin> 187 <plugin> 188 <groupId>org.apache.maven.plugins</groupId> 189 <artifactId>maven-compiler-plugin</artifactId> 190 <version>3.1</version> 191 <configuration> 192 <source>1.8</source> 193 <target>1.8</target> 194 <encoding>UTF-8</encoding><!-- 指定编码格式,否则在DOS下运行mvn compile命令时会出现莫名的错误,因为系统默认使用GBK编码 --> 195 </configuration> 196 </plugin> 197 <plugin> 198 <groupId>org.apache.maven.plugins</groupId> 199 <artifactId>maven-resources-plugin</artifactId> 200 <version>2.6</version> 201 <configuration> 202 <encoding>UTF-8</encoding><!-- 指定编码格式,否则在DOS下运行mvn命令时当发生文件资源copy时将使用系统默认使用GBK编码 --> 203 </configuration> 204 </plugin> 205 </plugins> 206 </build> 207 </project>
spring.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" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd" default-autowire="byName"> <!--注解支持 注意:一定要写在最前面--> <mvc:annotation-driven conversion-service="conversionService" validator="customerValidator" ></mvc:annotation-driven> <!-- 可扫描 controller、service --> <context:component-scan base-package="com.ballot.controller" /> <context:component-scan base-package="com.ballot.service" /> <!-- 数据源 使用 dbcp --> <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> <property name="driverClassName"> <value>com.mysql.jdbc.Driver</value> </property> <property name="url"> <value>jdbc:mysql://localhost:3306/temp123?characterEncoding=utf-8</value> </property> <property name="username" > <value>root</value> </property> <property name="password" > <value>g2ZFp6Yw</value> </property> <!-- 其他配置 --> <property name="initialSize" value="5"/> <property name="maxIdle" value="20" /> <property name="minIdle" value="5" /> </bean> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean" scope="prototype"> <!-- 加载 mybatis 配置文件 --> <!--<property name="configLocation" value="classpath:mybatis.xml"></property>--> <!-- 批量指定类型别名 --> <property name="typeAliasesPackage" value="com.ballot.entity" /> <!-- 数据源 --> <property name="dataSource" ref="dataSource" /> </bean> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <!-- 自动扫描 Mapper 遵循规范:需要将 mapper 接口类名 和 mapper.xml 映射文件名称保持一致,并放在一个目录中 多个包用半角逗号分隔 自动扫描出来的mapper的bean的id为mapper类名(首字母小写) --> <property name="basePackage" value="com.ballot.mapper"/> <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/> </bean> <!--spring 声明式事务管理--> <!-- mybatis 事务管理器 --> <bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource" /> </bean> <!-- 使用tx标签配置事务 --> <tx:advice id="txAdvice" transaction-manager="txManager"> <tx:attributes> <tx:method name="insert*" propagation="REQUIRED" rollback-for="Exception"/> <tx:method name="update*" propagation="REQUIRED" rollback-for="Exception"/> <tx:method name="delete*" propagation="REQUIRED" rollback-for="Exception"/> <tx:method name="*" propagation="REQUIRED" read-only="true"/> </tx:attributes> </tx:advice> <aop:config> <aop:pointcut id="daoMethod" expression="execution(* com.ballot.service.*.*(..))"/> <aop:advisor advice-ref="txAdvice" pointcut-ref="daoMethod" /> </aop:config> <!--静态资源解析--> <mvc:resources mapping="/admin/**" location="/admin/" /> <!-- 视图解析器 --> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" > <property name="prefix" value="/WEB-INF/jsp/"/> <property name="suffix" value=".jsp" /> </bean> <!--自定义参数绑定--> <bean id="conversionService" class="org.springframework.format.support.FormattingConversionServiceFactoryBean"> <property name="converters"> <set> <bean class="com.ballot.controller.converter.DateConverter" ></bean> </set> </property> </bean> <!-- 验证框架支持 --> <!-- 以下 validator ConversionService 在使用 mvc:annotation-driven 会 自动注册--> <bean id="customerValidator" class="org.springframework.validation.beanvalidation.LocalValidatorFactoryBean"> <property name="providerClass" value="org.hibernate.validator.HibernateValidator"/> <!-- 如果不加默认到 使用classpath下的 ValidationMessages.properties --> <property name="validationMessageSource" ref="messageSource"/> </bean> <!-- 国际化的消息资源文件(本系统中主要用于显示/错误消息定制) --> <bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource"> <property name="basenames"> <list> <value>classpath:message</value> </list> </property> <property name="defaultEncoding" value="UTF-8"/> <property name="cacheSeconds" value="120"/> </bean> <!-- 全局异常处理器 --> <bean class="com.ballot.exception.ExceptionHandler"/> <!--文件上传--> <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"> <!--设置最大上传为5M--> <property name="maxUploadSize"> <value>5242880</value> </property> </bean> <!--拦截器--> <mvc:interceptors> <mvc:interceptor> <!--/** 表示所有URL包括URL子路径--> <mvc:mapping path="/**"/> <bean class="com.ballot.interceptor.DemoInterceptor" /> </mvc:interceptor> </mvc:interceptors> </beans>
log4.properties
log4j.rootLogger=DEBUG,Console,File
log4j.appender.Console=org.apache.log4j.ConsoleAppender
log4j.appender.Console.Target=System.out
log4j.appender.Console.layout=org.apache.log4j.PatternLayout
log4j.appender.Console.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} [%t] %p [%C] - %m%n
log4j.logger.org.apache.ibatis.logging.jdbc.BaseJdbcLogge=DEBUG
log4j.logger.com.ballot=DEBUG
log4j.appender.File=org.apache.log4j.RollingFileAppender
log4j.appender.File.File=logs/ssm.log
log4j.appender.File.MaxFileSize=10MB
log4j.appender.File.Threshold=ALL
log4j.appender.File.layout=org.apache.log4j.PatternLayout
log4j.appender.File.layout.ConversionPattern=[%p][%t][%d{yyyy-MM-dd HH:mm:ss}][%C] - %m%n
web.xml
<web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0"> <display-name>Archetype Created Web Application</display-name> <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:spring.xml</param-value> </init-param> </servlet> <servlet-mapping> <servlet-name>springmvc</servlet-name> <!-- 1. *.action 解析指定路径 2. / , 对于静态文件配置 不让 DispatcherServlet 解析 3. /* , 使用这种配置 当 需要转发到 jsp 页面时 仍然会用 DispatcherServlet 解析jsp --> <url-pattern>/</url-pattern> </servlet-mapping> <!--乱码处理过滤器--> <filter> <filter-name>CharacterEncodingFilter</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>CharacterEncodingFilter</filter-name> <url-pattern>/</url-pattern> </filter-mapping> </web-app>
generatorConfig.xml( mybatis逆向工具用,请勿自定义文件名 )
1 <?xml version="1.0" encoding="UTF-8"?> 2 <!DOCTYPE generatorConfiguration PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN" 3 "http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd"> 4 <generatorConfiguration> 5 <!-- 驱动包路径! --> 6 <classPathEntry location="E:jarmysql-connector-java-5.1.38-bin.jar"/> 7 <context id="MysqlTables" targetRuntime="MyBatis3Simple"> 8 <!-- 生成的Java文件的编码 --> 9 <property name="javaFileEncoding" value="UTF-8"/> 10 <!-- beginningDelimiter和endingDelimiter:指明数据库的用于标记数据库对象名的符号,比如ORACLE就是双引号,MYSQL默认是`反引号; --> 11 <property name="beginningDelimiter" value="`"/> 12 <property name="endingDelimiter" value="`"/> 13 14 <!-- java类型处理器 15 用于处理DB中的类型到Java中的类型,默认使用JavaTypeResolverDefaultImpl; 16 注意一点,默认会先尝试使用Integer,Long,Short等来对应DECIMAL和 NUMERIC数据类型; 17 --> 18 <javaTypeResolver type="org.mybatis.generator.internal.types.JavaTypeResolverDefaultImpl"> 19 <!-- 20 true:使用BigDecimal对应DECIMAL和 NUMERIC数据类型 21 false:默认, 22 scale>0;length>18:使用BigDecimal; 23 scale=0;length[10,18]:使用Long; 24 scale=0;length[5,9]:使用Integer; 25 scale=0;length<5:使用Short; 26 --> 27 <property name="forceBigDecimals" value="false"/> 28 </javaTypeResolver> 29 30 31 32 <commentGenerator> 33 <property name="suppressAllComments" value="true" /> 34 </commentGenerator> 35 <!-- jdbc url地址 ! --> 36 <jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://localhost:3306/temp123?generateSimpleParameterMetadata=true" userId="root" password="g2ZFp6Yw"> 37 </jdbcConnection> 38 39 <!-- targetPackage 生成后的路径 ,targetProject 绝对路径 该配置生成java代码 --> 40 <javaModelGenerator targetPackage="com.ballot.entity" targetProject=".srcmainjava"> 41 <property name="enableSubPackages" value="true"/> 42 <property name="trimStrings" value="true"/> 43 <!-- 设置是否在getter方法中,对String类型字段调用trim()方法 --> 44 <property name="trimStrings" value="true"/> 45 </javaModelGenerator> 46 <!-- 该配置生成 mybatis 映射文件 --> 47 <sqlMapGenerator targetPackage="com.ballot.mapper" targetProject=".srcmainjava"> 48 <property name="enableSubPackages" value="true"/> 49 </sqlMapGenerator> 50 <!-- 生成 java 接口代码 --> 51 <javaClientGenerator type="XMLMAPPER" targetPackage="com.ballot.mapper" targetProject=".srcmainjava"> 52 <property name="enableSubPackages" value="true"/> 53 </javaClientGenerator> 54 <!-- 表配置 tableName 表名 domainObjectName java类名,首字母必须大写,否则报字符串越界错误 --> 55 <table tableName="test_model" domainObjectName="Tuota" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"> 56 <property name="useActualColumnNames" value="true"/> 57 </table> 58 </context> 59 60 </generatorConfiguration>
注:资源目录下需要建立message.properties文件供hibernate-validator使用