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>
applicationContext.xml
<?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>
EmpMapper.xml
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;

  

猜你喜欢

转载自www.cnblogs.com/erbinok/p/9090400.html
今日推荐