首页 > 其他 > 详细

Mybatis的配置

时间:2016-02-23 13:02:56      阅读:188      评论:0      收藏:0      [点我收藏+]

1.创建一个实体包entity,用来与数据库中表一一对应

 

 

2.创建一个DAO包,用来处理对应的表操作

@Component(value="adminDAOImpl")
public class AdminDAOImpl implements AdminDAO {

private AdminUserMapper adminUserMapper;

@Override
public void save(Admin user) {

}

/**
*
* @param adminUser 提交的表单admin的信息
*/
@Override
public Admin find(Admin adminUser) {
Map<String, Object> param = new HashMap<String, Object>();
param.put("username", adminUser.getUsername());
param.put("password", adminUser.getPassword());

return adminUserMapper.getAdminUser(param);
}

public AdminUserMapper getAdminUserMapper() {
return adminUserMapper;
}

@Resource(name="adminUserMapper")
public void setAdminUserMapper(AdminUserMapper adminUserMapper) {
this.adminUserMapper = adminUserMapper;
}

/**
* 获取所有Admin信息
*/
@Override
public PageBean<Admin> getAdminAll(int pc)
{
/*
* 1. 得到ps
* 2. 得到tr
* 3. 得到beanList
* 4. 创建PageBean,返回
*/

/*
* 1. 得到ps
*/
int ps = PageConstants.ORDER_PAGE_SIZE;//每页记录数

/*
* 3. 总记录数
*/
int tr = adminUserMapper.getAdminAllNum();//得到了总记录数

/*
* 4. 得到beanList,即当前页记录
*/

// Map<Integer, Object> map = new HashMap<Integer, Object>();
// map.put("curpage", 8 * (pc -1));

List<Admin> beanList = adminUserMapper.adminPageData(pc);


/*
* 5. 创建PageBean,设置参数
*/
PageBean<Admin> pb = new PageBean<Admin>();
/*
* 其中PageBean没有url,这个任务由Servlet完成
*/
pb.setBeanList(beanList);
pb.setPc(pc); //当前页
pb.setPs(ps); //每页记录数
pb.setTr(tr); //总记录数

return pb;
}

@Override
public void modifyPassword(Admin adminUser) {
Map<String, Object> param = new HashMap<String, Object>();
param.put("username", adminUser.getUsername());
param.put("password", adminUser.getPassword());
param.put("newPassword", adminUser.getNewPassword());

this.adminUserMapper.modifyPassword(param);
}

@Override
public Admin getAdminUserInfo(String user) {
return this.adminUserMapper.getAdminUserInfo(user);
}

@Override
public void updateAdminUser(Admin admin) {
this.adminUserMapper.updateAdminUser(admin);
}

}

 

3.创建对应的映射接口文件,用来执行增删改查的操作,可以创建一个单独的映射包mapper

public interface AdminUserMapper {

/**
* 获取管理员用户用户名和密码来查询管理员是否存在
* @param adminInfo 是key-value结构,使用容器来传递数据信息, key代表表的字段,value是值
* @return
*/
public Admin getAdminUser(Map<String, Object> adminInfo);

/**
* 获取所有管理员信息
* @return
*/
public List<Admin> getAdminAll();

/**
* 查询总计有多少条管理员记录
* @return
*/
public Integer getAdminAllNum();

/**
* 查找当前页面的管理员列表
* @param curPage
* @return
*/
public List<Admin> adminPageData(Integer curPage);

/**
* 修改管理员密码
* @param adminInfo
*/
public void modifyPassword(Map<String, Object> adminInfo);

/**
* 根据用户名查找管理员信息
* @param user
* @return
*/
public Admin getAdminUserInfo(String user);

/**
* 更新管理员信息
* @param admin
* @return
*/
public Integer updateAdminUser(Admin admin);
}

 

4.配置文件的设置

1.mybatis的文件配置

<?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>
<typeAlias type="com.hyd.carloan.business.merchant.admin.entity.Admin" alias="admin"/>

<typeAlias type="com.hyd.carloan.business.merchant.category.entity.Category" alias="category"/>

<typeAlias type="com.hyd.carloan.business.merchant.front.entity.T_user" alias="frontUser"/>

<typeAlias type="com.hyd.carloan.business.merchant.borrow.entity.Borrow" alias="borrow"/>
</typeAliases>
<mappers>
<mapper resource="/mybatis/AdminUserTable.xml"></mapper>
<mapper resource="/mybatis/UserTable.xml"></mapper>
<mapper resource="/mybatis/CategoryTable.xml"></mapper>
<mapper resource="/mybatis/BorrowTable.xml"></mapper>
</mappers>

</configuration>

 

上面是别名,下面是对应表的操作文件

 

2.操作表的配置文件

<?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.hyd.carloan.business.merchant.admin.dao.AdminUserMapper">
<resultMap type="Admin" id="AdminResultMap">
<id property="id" column="id" />
<result property="username" column="username" />
<result property="password" column="password" />
<result property="enable" column="enable" />
<result property="lastLoginTime" column="lastLoginTime" />
<result property="lastLoginIP" column="lastLoginIP" />
<result property="roleId" column="roleId" />
<result property="realName" column="realName" />
<result property="telphone" column="telphone" />
<result property="qq" column="qq" />
<result property="email" column="email" />
<result property="img" column="img" />
<result property="isLeader" column="isLeader" />
<result property="sex" column="sex" />
<result property="idCard" column="idCard" />
<result property="summary" column="summary" />
<result property="nativePlacePro" column="nativePlacePro" />
<result property="nativePlaceCity" column="nativePlaceCity" />
<result property="address" column="address" />
<result property="addDate" column="addDate" />
<result property="moneys" column="moneys" />
</resultMap>

<!-- 用户登录,查询管理员用户名和密码是否存在 -->
<select id="getAdminUser" parameterType="java.util.Map" resultType="admin">
SELECT * from t_admin A
WHERE A.username = #{username} and A.password = #{password}
</select>

<!-- 查询管理员列表 -->
<select id="getAdminAll" resultMap="AdminResultMap" >
select DISTINCT id,userName,realName,telphone,lastLoginTime,lastLoginIP from t_Admin order by id
</select>

<!-- 查询管理员数目 -->
<select id="getAdminAllNum" resultType="java.lang.Integer" >
select count(*) from t_Admin
</select>

<!-- 修改密码 -->
<select id="modifyPassword" parameterType="java.util.Map" resultType="java.lang.Integer">
update t_admin set passoword = #{newpassword} where username = #{username}
</select>

<!-- 分页数据 -->
<select id="adminPageData" parameterType="java.lang.Integer" resultMap="AdminResultMap">
select top 8 * from t_admin where id not in (select top (8 * (#{curpage} - 1)) id from t_admin order by id) order by id
</select>

<!-- 根据用户名查找管理员信息 -->
<select id="getAdminUserInfo" parameterType="java.lang.String" resultType="admin">
select * from t_admin where username=#{username}
</select>

<!-- 修改密码 -->
<select id="updateAdminUser" parameterType="admin" resultType="java.lang.Integer">
update t_admin set password = #{password} , telphone=#{telphone},qq=#{qq} , email=#{email} where username = #{username}
</select>
</mapper>

 

1.映射的名称空间,就是接口的地址

<mapper namespace="com.hyd.carloan.business.merchant.admin.dao.AdminUserMapper">

 

2.返回的map结构,作为返回结果集

<resultMap type="Admin" id="AdminResultMap">
<id property="id" column="id" />
<result property="username" column="username" />
<result property="password" column="password" />
<result property="enable" column="enable" />
<result property="lastLoginTime" column="lastLoginTime" />
<result property="lastLoginIP" column="lastLoginIP" />
<result property="roleId" column="roleId" />
<result property="realName" column="realName" /> 
<result property="telphone" column="telphone" />
<result property="qq" column="qq" />
<result property="email" column="email" />
<result property="img" column="img" /> 
<result property="isLeader" column="isLeader" />
<result property="sex" column="sex" />
<result property="idCard" column="idCard" />
<result property="summary" column="summary" />
<result property="nativePlacePro" column="nativePlacePro" />
<result property="nativePlaceCity" column="nativePlaceCity" />
<result property="address" column="address" />
<result property="addDate" column="addDate" />
<result property="moneys" column="moneys" />
</resultMap>

 

3.设置参数的类型和返回值的类型

<!-- 用户登录,查询管理员用户名和密码是否存在 -->
<select id="getAdminUser" parameterType="java.util.Map" resultType="admin">
SELECT * from t_admin A
WHERE A.username = #{username} and A.password = #{password}
</select>

4.如果参数类型是Map结构的话,需要提前处理

public void modifyPassword(Admin adminUser) {
Map<String, Object> param = new HashMap<String, Object>();
param.put("username", adminUser.getUsername());
param.put("password", adminUser.getPassword());
param.put("newPassword", adminUser.getNewPassword());

this.adminUserMapper.modifyPassword(param);
}

 

Mybatis的配置

原文:http://www.cnblogs.com/handsome1013/p/5209334.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!