spring-mybatis整合
1.请使用支持 JDBC 4.0 的 sqljdbc4.jar 类库 java.lang.UnsupportedOperationException: 此驱动程序不支持 Java Runtime Environment (JRE) 1.6 版。请使用支持 JDBC 4.0 的 sqljdbc4.jar 类库。
解决方案:JDBC2.0驱动程序有两个jar包,分别为sqljdbc.jar和sqljdbc4.jar,只用把sqljdbc4.jar拷贝到tomcat\lib文件夹下就行了,不要sqljdbc.jar这个jar包。
2.sqlServer2008 R2 JDBC配置:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
<?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:context="http://www.springframework.org/schema/context" xmlns:jdbc="http://www.springframework.org/schema/jdbc" xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:jpa="http://www.springframework.org/schema/data/jpa" xmlns:util="http://www.springframework.org/schema/util" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.2.xsd http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.2.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd"> <bean id="ds" class="org.apache.commons.dbcp.BasicDataSource"> <property name="driverClassName" value="com.microsoft.sqlserver.jdbc.SQLServerDriver"/> <property name="url" value="jdbc:sqlserver://localhost:1433;DatabaseName=testdb;"/> <property name="username" value="sa"/> <property name="password" value="111111"/> </bean> <!-- 配置SqlSessionFactory --> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="ds"/> <property name="mapperLocations" value="classpath:com/zixue/entity/*.xml"/> </bean> <!-- 配置MyBatis注解 --> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value="com.zixue.dao"/> <property name="annotationClass" value="com.zixue.annotation.MyBatisRepository"/> </bean> <!-- 开启注解扫描 --> <context:component-scan base-package="com.zixue"/> <!-- 开启RequestMapping注解 --> <mvc:annotation-driven/> <!-- 处理请求转发 --> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/jsp/"/> <property name="suffix" value=".jsp"/> </bean> </beans>
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE mapper PUBLIC "-//ibatis.apache.org//DTD Mapper 3.0//EN" "http://ibatis.apache.org/dtd/ibatis-3-mapper.dtd"> <mapper namespace="com.zixue.dao.EmpDao"> <!-- 保存一条员工的数据 --> <insert id="save" parameterType="com.zixue.entity.Emp" keyProperty="empno" useGeneratedKeys="true"> insert into t_emp values( #{ename}, #{job,}, #{mgr}, #{hiredate}, #{sal}, #{comm}, #{deptno} ) </insert> </mapper>
create table t_emp( empno int identity(1,1) primary key, ename varchar(20), job varchar(10), mgr int, hiredate date, sal decimal(9,2), comm decimal(9,2), deptno int );
3.MySQL JDBC配置:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
<?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:context="http://www.springframework.org/schema/context" xmlns:jdbc="http://www.springframework.org/schema/jdbc" xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:jpa="http://www.springframework.org/schema/data/jpa" xmlns:util="http://www.springframework.org/schema/util" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.2.xsd http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.2.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd"> <bean id="ds" class="org.apache.commons.dbcp.BasicDataSource"> <property name="driverClassName" value="com.mysql.jdbc.Driver"/> <property name="url" value="jdbc:mysql://localhost:3306/testdb"/> <property name="username" value="root"/> <property name="password" value="root"/> </bean> <!-- 配置SqlSessionFactory --> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="ds"/> <property name="mapperLocations" value="classpath:com/zixue/entity/*.xml"/> </bean> <!-- 配置MyBatis注解 --> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value="com.zixue.dao"/> <property name="annotationClass" value="com.zixue.annotation.MyBatisRepository"/> </bean> <!-- 开启注解扫描 --> <context:component-scan base-package="com.zixue"/> <!-- 开启RequestMapping注解 --> <mvc:annotation-driven/> <!-- 处理请求转发 --> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/jsp/"/> <property name="suffix" value=".jsp"/> </bean> </beans>
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE mapper PUBLIC "-//ibatis.apache.org//DTD Mapper 3.0//EN" "http://ibatis.apache.org/dtd/ibatis-3-mapper.dtd"> <mapper namespace="com.zixue.dao.EmpDao"> <!-- 保存一条员工的数据 --> <insert id="save" parameterType="com.zixue.entity.Emp" keyProperty="empno" useGeneratedKeys="true"> insert into t_emp (ename,job,mgr,hiredate,sal,comm,deptno) values( #{ename}, #{job,}, #{mgr}, #{hiredate}, #{sal}, #{comm}, #{deptno} ) </insert> </mapper>
create table t_emp( empno int primary key auto_increment, ename varchar(20), job varchar(10), mgr int, hiredate date, sal double, comm double, deptno int ) AUTO_INCREMENT=100;
4.Oracle JDBC 配置:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
<?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:context="http://www.springframework.org/schema/context" xmlns:jdbc="http://www.springframework.org/schema/jdbc" xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:jpa="http://www.springframework.org/schema/data/jpa" xmlns:util="http://www.springframework.org/schema/util" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.2.xsd http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.2.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd"> <bean id="ds" class="org.apache.commons.dbcp.BasicDataSource"> <property name="driverClassName" value="oracle.jdbc.driver.OracleDriver"/> <property name="url" value="jdbc:oracle:thin:@localhost:1521:orcl"/> <property name="username" value="test"/> <property name="password" value="test"/> </bean> <!-- 配置SqlSessionFactory --> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="ds"/> <property name="mapperLocations" value="classpath:com/zixue/entity/*.xml"/> </bean> <!-- 配置MyBatis注解 --> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value="com.zixue.dao"/> <property name="annotationClass" value="com.zixue.annotation.MyBatisRepository"/> </bean> <!-- 开启注解扫描 --> <context:component-scan base-package="com.zixue"/> <!-- 开启RequestMapping注解 --> <mvc:annotation-driven/> <!-- 处理请求转发 --> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/jsp/"/> <property name="suffix" value=".jsp"/> </bean> </beans>
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE mapper PUBLIC "-//ibatis.apache.org//DTD Mapper 3.0//EN" "http://ibatis.apache.org/dtd/ibatis-3-mapper.dtd"> <mapper namespace="com.zixue.dao.EmpDao"> <!-- 保存一条员工数据 --> <insert id="save" parameterType="com.zixue.entity.Emp"> <selectKey keyProperty="empno" order="BEFORE" resultType="java.lang.Integer"> select emp_seq.nextval from dual </selectKey> insert into t_emp values( #{empno}, #{ename,jdbcType=VARCHAR}, #{job,jdbcType=VARCHAR}, #{mgr,jdbcType=NUMERIC}, #{hiredate,jdbcType=DATE}, #{sal,jdbcType=NUMERIC}, #{comm,jdbcType=NUMERIC}, #{deptno,jdbcType=NUMERIC} ) </insert> </mapper>
create table t_emp( empno number(4) primary key, ename varchar(20), job varchar(10), mgr number(4), hiredate date, sal number(9,2), comm number(9,2), deptno number(4) ); create sequence emp_seq start with 100; select emp_seq.nextval from dual; Alter session set NLS_DATE_FORMAT='yyyy mm dd hh24:mi:ss'
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
package com.zixue.test; import java.util.Date; import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import com.zixue.dao.EmpDao; import com.zixue.entity.Emp; public class TestMapping { @Test public void test1(){ ApplicationContext ac =new ClassPathXmlApplicationContext("applicationContext.xml"); EmpDao dao =ac.getBean(EmpDao.class); Emp e =new Emp(); e.setEname("张三"); e.setJob("SEALSMAN"); e.setMgr(3); e.setHiredate(new Date(System.currentTimeMillis())); e.setSal(40000.00); e.setComm(650.0); e.setDeptno(10); dao.save(e); System.out.println(e.getEmpno()); } }