Spring整合MyBatis使用动态代理实现MySQL数据测试的工程配置

1 lib下Spring与MyBatis等JAR包,实在有点多,整合嘛,忍忍!
在这里插入图片描述
2 applicationContext-mybatis.xml配置如下:其中有mapper代理和扫描包形式配置mapper,Mapper代理被我注释掉了。

<?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:aop="http://www.springframework.org/schema/aop"
	xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context"
	xsi:schemaLocation="
 http://www.springframework.org/schema/beans  
 http://www.springframework.org/schema/beans/spring-beans.xsd
 http://www.springframework.org/schema/aop 
 http://www.springframework.org/schema/aop/spring-aop.xsd
 http://www.springframework.org/schema/context 
 http://www.springframework.org/schema/context/spring-context.xsd
 ">

	<context:property-placeholder location="classpath:/db.properties" />
	<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
		<property name="url" value="${jdbc.url}"></property>
		<property name="driverClassName" value="${jdbc.driverClass}"></property>
		<property name="username" value="${jdbc.username}"></property>
		<property name="password" value="${jdbc.password}"></property>
		<property name="maxActive" value="${jdbc.maxActive}"></property>
	</bean>

	<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
		<property name="configLocation" value="classpath:SqlMapConfig.xml"></property>
		<property name="dataSource" ref="dataSource"></property>
	</bean>

	<!--Mapper代理 <bean id="userMapper" class="org.mybatis.spring.mapper.MapperFactoryBean"> 
		<property name="mapperInterface" value="com.sea.crm.Dao.UserMapper"></property> 
		<property name="sqlSessionFactory" ref="sqlSessionFactory"></property> </bean> -->
	<!-- 扫描包方式配置代理 -->
	<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
		<property name="basePackage" value="com.sea.crm.mapper"></property>
	</bean>
	<context:component-scan base-package="com.sea.crm"></context:component-scan>
	<import resource="classpath:/applicationContext-tran.xml" />
</beans>

3 SqlMapConfig.xml配置如下:

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<typeAliases>
		<!-- 给类型 com.igeek.crm.pojo.User取别名user-->
		<typeAlias type="com.sea.crm.pojo.User" alias="user"/>
		<!-- 配置一个包,让该包中所有的类都是用简称 -->
		<package name="com.sea.crm.pojo"/>
	</typeAliases>
	<!-- 将映射文件配置到mybatis的配置文件中 -->
	<mappers>
		<!--<mapper resource="sqlmap/user.xml"/> -->
		<!--
		 <mapper class="com.sea.crm.mapper.UserMapper"/>
		 -->
		 
		<package name="com.sea.crm.mapper"/>
	</mappers>
</configuration>

4 UserMapper.xml数据库代码如下:

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.sea.crm.mapper.UserMapper">
	<!-- 根据用户id查询 -->
	<select id="queryUserById" parameterType="int" resultType="user">
		select * from user where id = #{id}
	</select>

	<!-- 根据用户名模糊查询用户 -->
	<select id="queryUserByUsername" parameterType="string"
		resultType="user">
		select * from user where username like '%${value}%'
	</select>

	<!-- 添加用户 -->
	<insert id="saveUser" parameterType="user">
		<selectKey keyProperty="id" keyColumn="id" order="AFTER"
			resultType="int">
			select last_insert_id()
		</selectKey>
		insert into user
		(username,birthday,sex,address)
		values
		(#{username},#{birthday},#{sex},#{address})
	</insert>

</mapper>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值