ssm
spring + springmvc + mybatis 集成
一.创建项目
二.导入jar包
- mybatis 的 jar包
- spring 的 jar 包
- springmvc的jar包
db.properties
配置spring
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
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
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
">
<!--扫描包-->
<context:component-scan base-package="cn.itsource.domain"/>
<context:component-scan base-package="cn.itsource.service"/>
<!--引入jdbc.properties-->
<context:property-placeholder location="classpath:jdbc.properties"/>
<!--配置四连接-->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="${jdbc.driverClassName}"></property>
<property name="url" value="${jdbc.url}"></property>
<property name="username" value="${jdbc.username}"></property>
<property name="password" value="${jdbc.password}"></property>
</bean>
<!--集成mydatis-->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<!--取别名-->
<property name="typeAliasesPackage" value="cn.itsource.mapper.*"/>
<property name="mapperLocations" value="classpath:cn/itsource/mapper/*.xml" />
</bean>
<!--启动事务-->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource" />
</bean>
<tx:annotation-driven transaction-manager="transactionManager"/>
<!--注入映射器-->
<bean id="userMapper" class="org.mybatis.spring.mapper.MapperFactoryBean">
<property name="mapperInterface" value="cn.itsource.mapper.EmployeeMapper" />
<property name="sqlSessionFactory" ref="sqlSessionFactory" />
</bean>
</beans>4
配置springmvc
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
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
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
">
<!--扫描包-->
<context:component-scan base-package="cn.itsource.controller"/>
<!--开启静态资源访问-->
<mvc:default-servlet-handler/>
<!--开启注解支持-->
<mvc:annotation-driven/>
<!--视图解析器-->
<bean id="internalResourceViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/views/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
<!--
上传下载
id必须是multipartResolver
-->
<bean id="multipartResolver"
class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<!-- 上传文件大小上限,单位为字节(10MB) -->
<property name="maxUploadSize">
<value>10485760</value>
</property>
<!-- 请求的编码格式,必须和jSP的pageEncoding属性一致,以便正确读取表单的内容,默认为ISO-8859-1 -->
<property name="defaultEncoding">
<value>UTF-8</value>
</property>
</bean>
</beans>
web.xml配置
<?xml version="1.0" encoding="UTF-8"?>
<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">
<!--编码问题-->
<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>
<!--spring核心配置-->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:aplicationContext.xml</param-value>
</context-param>
<!--监听器-->
<listener>
<listener-class>org.springframework.test.context.ContextConfiguration</listener-class>
</listener>
<!--springmvc的核心配置-->
<servlet>
<servlet-name>dispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!--设置启动优先级-->
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcherServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
2.mybatis代码生成器的学习使用
- 需要的jar包
<dependencies>
<!--mybatis依赖包-->
<!--mybatis核心包-->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.2.1</version>
</dependency>
<!--jdbc驱动包-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.46</version>
</dependency>
<!--test-->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
</dependencies>
<build>
<plugins>
<!--代码生成器插件-->
<plugin>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-maven-plugin</artifactId>
<version>1.3.2</version>
<configuration>
<!--自定义代码生成器的路径-->
<!--<configurationFile>yourLocation/mybatis-generator-config.xml</configurationFile>-->
<verbose>true</verbose>
<overwrite>true</overwrite>
</configuration>
</plugin>
<!-- 局部jdk配置,pom.xml中 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
-
创建xml
-
配置文件内容
只需要修改:
- classPathEntry location=“D:\java\mysql_jar\mysql-connector-java-5.1.26-bin.jar”/
- 数据库的四大连接
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE generatorConfiguration
PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN"
"http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd">
<!-- 自动生成器的配置(根目录,不做过多介绍)-->
<generatorConfiguration>
<!--
classPathEntry:可以配置多个,也不配置
数据库驱动:这里找到相应的驱动jar包就可以了(注:不同数据库的jar不一样)
location:里面的是路径(也可以直接写绝对路径 -> 如:E:\mybatis\mysql-connector-java-5.1.26-bin.jar)
-->
<classPathEntry location="D:\java\mysql_jar\mysql-connector-java-5.1.26-bin.jar"/>
<!--
context:用于生成一组对象的环境(至少配置1个,可以配置多个)
id:表达唯一的名称
targetRuntime:用于指定生成的代码的运行环境(MyBatis3/MyBatis3Simple)
MyBatis3:默认值
MyBatis3Simple:不会生成与Example(案例)相关的方法
-->
<context id="DB2Tables" targetRuntime="MyBatis3Simple" >
<!--
用于配置如果生成注释信息(最多可以配置一下)
suppressAllComments:阻止生成注释 ,默认为false
suppressDate:阻止生成的注释 时间戳,默认为false
addRemarkComments:注释是否添加数据库表的备注信息,默认为false
-->
<commentGenerator>
<property name="suppressDate" value="true"/>
<property name="suppressAllComments" value="true"/>
</commentGenerator>
<!--
这个应该比较清楚,配置连接数据库的基本信息
-->
<jdbcConnection driverClass="com.mysql.jdbc.Driver"
connectionURL="jdbc:mysql:///mybatis"
userId="root" password="123456">
</jdbcConnection>
<!--
用于指定JDBC类型和Java类型如何转换,最多可以配置一个
forceBigDecimals:控制是否强制将DECIMAL和NUMERIC类型的JDBC字段转换成Java类型的 BigDecimal
默认为false,一般不需要配置
-->
<javaTypeResolver>
<property name="forceBigDecimals" value="false"/>
</javaTypeResolver>
<!--
javaModelGenerator:用来控制生成的实体类
targetPackage:生成Model类存放位置(包名)
targetProject:指定目标项目路径(根目录)
对应的子属性:
trimStrings:判断是否对数据库查询结果进行trim操作(默认false)
-->
<javaModelGenerator targetPackage="cn.itsource.wj.domain" targetProject="src/main/java">
<property name="trimStrings" value="true"/>
</javaModelGenerator>
<!--
sqlMapGenerator:生成映射文件存放位置(Mapper.xml文件)
targetPackage:生成SQL映射文件(XML文件)在哪个包中
targetProject:指定目标项目路径(根目录)
-->
<sqlMapGenerator targetPackage="cn.itsource.wj.mapper" targetProject="src/main/resources">
</sqlMapGenerator>
<!--
javaClientGenerator:Java客户端生成器(生成Dao/Mapper的接口)
该 标签可选(最多配置一个),如果不配置,就不会生成Mapper接口
type:选择客户端代码生成器
MyBatis3
ANNOTATEDMAPPER:基于注解的Mapper接口,不会有对应的XML映射文件
MIXEDMAPPER:XML和注解混合形式
XMLMAPPER:所有方法都在XML中(接口调用依赖XML)
MyBatis3Simple
ANNOTATEDMAPPER:基于注解的Mapper接口,不会有对应的XML映射文件
XMLMAPPER:所有方法都在XML中(接口调用依赖XML)
targetPackage:生成Mapper接口存放的包名
targetProject:指定目标项目路径
-->
<javaClientGenerator type="XMLMAPPER" targetPackage="cn.itsource.wj.mapper" targetProject="src/main/java">
<property name="enableSubPackages" value="true"/>
</javaClientGenerator>
<!--
table:生成对应表及类名
tableName:对应表名(注:%代表所有)
domainObjectName:对应的类名
generatedKey:主键自增的id字段(针对当前 数据库配置MySQL)
-->
<table tableName="employee" domainObjectName="Employee">
<generatedKey column="id" sqlStatement="MySql" />
</table>
</context>
</generatorConfiguration>