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>