spring-mybatis整合
Posted erbinok
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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配置:
<?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>
<?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配置:
<?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>
<?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;
以上是关于spring-mybatis整合的主要内容,如果未能解决你的问题,请参考以下文章
最新最全面的Spring详解——Spring-Mybatis整合
Spring-Mybatis --- 配置SqlSessionFactoryBean,整合Spring-Mybatis(转)