首先web.xml里配置spring
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
applicationContext.xml中,用util标签加入jdbc配置文件,
<util:properties id="propertiesReader" location="classpath:jdbc.properties" />
开启注解与事务
<context:annotation-config />
<context:component-scan base-package="com.auth" />
<tx:annotation-driven transaction-manager="transactionManager" />
<bean id="dataSource" class="org.logicalcobwebs.proxool.ProxoolDataSource">
<property name="driver">
<value>com.mysql.jdbc.Driver</value>
</property>
<property name="driverUrl">
<value>jdbc:mysql://10.1.81.212:3306/test2</value>
</property>
<property name="user" value="root" />
<property name="password" value="root" />
<property name="alias" value="Pool_dbname" />
<property name="houseKeepingTestSql" value="select CURRENT_DATE" />
</bean>
<bean id="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource" />
</bean>
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="configLocation" value="classpath:MyBatis-Configuration.xml"></property>
<property name="dataSource" ref="dataSource" />
</bean>
<bean id="userDao" class="org.mybatis.spring.mapper.MapperFactoryBean">
<property name="mapperInterface" value="com.auth.dao.UserDao" />
<property name="sqlSessionFactory" ref="sqlSessionFactory"></property>
</bean>
再看MyBatis-Configuration.xml,指定一些属性和mapper
<?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>
<properties>
<property name="dialect" value="mysql" />
</properties>
<typeAliases>
<typeAlias type="com.security.management.core.entity.User" alias="User"/>
<typeAlias type="com.security.management.core.entity.Group" alias="Group"/>
<typeAlias type="com.security.management.core.entity.Role" alias="Role"/>
</typeAliases>
<mappers>
<mapper resource="com/auth/dao/UserDao.xml"/>
</mappers>
</configuration>
UserDao.xml里,配置resultMap
<?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.cs2c.esb.security.dao.UserDao">
<resultMap type="User" id="userResult">
<id column="uid" property="id" />
<result column="username" property="userName" />
<result column="enable" property="enabled" />
<result column="email" property="email" />
<result column="description" property="description" />
<result column="realname" property="realName" />
<collection property="roles" ofType="Role">
<id column="rid" property="id" />
<result column="rolename" property="roleName" />
</collection>
</resultMap>
<select id="findUserAndRoleByPage" parameterType="User" resultMap="userResult">
select u.id as uid, u.username ,u.enabled,u.email,u.description,u.realname, r.rolename,r.id as rid from esb_users u,esb_roles r,esb_user_role ur where u.id=ur.user_id and r.id = ur.role_id
<if test="users !=null and users.size()!=0">
and u.id in
<foreach item="item" index="index" collection="users" open="(" separator="," close=")">
#{item.id}
</foreach>
</if>
</select>
</mapper>
再看UserDao.java,它是一个接口
可以把SQL用注解写,也可以使用配置文件,例如findUserAndRoleByPage方法,就是在配置文件里配置的SQL方法
/**
* 处理USER的DAO层接口类,由MyBiatis动态代理实现。
*
*/
public interface UserDao {
/**
* 查询指定的用户名的用户
*
* @param name
* 用户名
* @return 用户
*/
@Select("SELECT * FROM " + EntitySqlMapping.USERS_TABLE_NAME + " u WHERE u.username = #{name}"
+ ";")
public User loadByUserName(String name);
/**
* @param RowBounds
* 分页参数,要指定页面大小和游标位置。 查询一页用户
* @return 一页的用户
*/
public List<User> findUserAndRoleByPage(@Param("users") List<User> users);
/**
* 删除指定ID的用户
* @param id
* 用户ID
*/
@Delete("DELETE FROM " + EntitySqlMapping.USERS_TABLE_NAME + " WHERE "
+ EntitySqlMapping.USERS_TABLE_NAME + ".id = #{id}" + ";")
public void deleteById(String id);
/**
* 添加用户
*
* @param user
* 用户
* @param groupId
* 组ID
*/
/*
* @Insert(
* "INSERT INTO users( id , username , passwd , enabled, group_admin ,password_type ,group_id ) "
* +
* "VALUES ( #{user.id} , #{user.userName} , #{user.passwd} , #{user.enabled}, #{user.groupAdmin} ,#{user.passwordType} ,#{groupId} )"
* ) public void allocate(@Param("user")User user,@Param("groupId")String
* groupId);
*/
/**
* 关联用户的角色的关系(因为是多对多关联,所以要添加这一步)
*
* @param uid
* 用户ID
* @param rid
* 角色ID
*/
@Insert("INSERT INTO " + EntitySqlMapping.USERS_ROLES_TABLE_NAME
+ "( user_id , role_id ) VALUES ( #{uid} , #{rid} );")
public void relevance(@Param("uid") String uid, @Param("rid") String rid);
/**
* 删除用户和角色的关系(因为是多对多关联,所以要添加这一步)
*
* @param uid
* 用户ID
*/
@Delete("DELETE FROM " + EntitySqlMapping.USERS_ROLES_TABLE_NAME + " WHERE "
+ EntitySqlMapping .USERS_ROLES_TABLE_NAME + ".user_id = #{uid};")
public void delRelevance(@Param("uid") String uid);
/**
* 删除指定的用户和角色的关系
*
* @param uid 用户ID
* @param rid 角色ID
*/
@Delete("DELETE FROM " + EntitySqlMapping.USERS_ROLES_TABLE_NAME + " WHERE "
+ EntitySqlMapping.USERS_ROLES_TABLE_NAME + ".user_id = #{uid} AND " + EntitySqlMapping.USERS_ROLES_TABLE_NAME + ".role_id = " + "#{rid};")
public void delURRelevance(@Param("uid") String uid, @Param("rid") String rid);
}