mybatis spring里配置ibatis

首先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" />


配置一个dataSource,这里使用proxool连接池
<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>


配置事务,和sqlSessionFactory,使用刚才创建的datasource,并指定mybatis-configuration
<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>


配置DAO,指定属性

	<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);
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

day walker

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值