..._getusercountbyroleid为根据角色id查询该角色下是否有用户信息。返回该角色id下的">
赞
踩
SQL映射配置文件
<?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="cn.com.yan.mapper.UserMapper">
<!--修改信息-->
<!--<update id="modify" parameterType="User">
update smbms_user set username=#{username},address=#{address} where id=#{id}
</update>-->
<!--使用if+set改造更新操作-->
<!--<update id="modify" parameterType="User">
update smbms_user
<set>
<if test="username!=null">username=#{username},</if>
<if test="address!=null">address=#{address}</if>
</set>
where id=#{id}
</update>-->
<!--使用if+trim替代if+set更新用户-->
<update id="modify" parameterType="User">
update smbms_user
<trim prefix="set" suffixOverrides="," suffix="where id=#{id}">
<if test="username!=null">username=#{username},</if>
<if test="address!=null">address=#{address}</if>
</trim>
</update>
<!--多条件查询用户表中的信息-->
<!--<select id="getuserlist" resultType="User">
select u.id,u.username,u.userpassword,u.phone,u.address from smbms_user u,smbms_role r
where u.userrole=r.id
<if test="username!=null and username!=''">
and username like concat('%',#{username},'%')
</if>
<if test="userrole!=null and userrole!=0">
and userrole=#{userrole}
</if>
</select>-->
<!--错误的例子-->
<!--<select id="getuserlist" resultType="User">
select * from smbms_user u where
<if test="username!=null and username!=''">
and username like concat('%',#{username},'%')
</if>
<if test="userrole!=null and userrole!=0">
and userrole=#{userrole}
</if>
</select>-->
<!--第一种更正以上错误的方法 -->
<!--<select id="getuserlist" resultType="User">
select * from smbms_user u
<where>
<if test="username!=null and username!=''">
and username like concat('%',#{username},'%')
</if>
<if test="userrole!=null and userrole!=0">
and userrole=#{userrole}
</if>
</where>
</select>-->
<!--第二种更正以上错误的方法-->
<select id="getuserlist" resultType="User">
select * from smbms_user u
<trim prefix="where" prefixOverrides="and | or">
<if test="username!=null and username!=''">
and username like concat('%',#{username},'%')
</if>
<if test="userrole!=null and userrole!=0">
and userrole=#{userrole}
</if>
</trim>
</select>
<!--根据用户角色列表。获取该角色列表下用户列表信息-foreach-array-->
<select id="getUserByRoleId_foreach_array" resultType="User">
select * from smbms_user where userrole in
<foreach collection="array" item="roleIds"
open ="(" separator="," close=")">
#{roleIds}
</foreach>
</select>
<!--根据用户角色列表。获取该角色列表下用户列表信息-foreach-list-->
<select id="getUserByRoleId_foreach_list" resultType="User">
select * from smbms_user where userrole in
<foreach collection="list" item="rolelist"
open ="(" separator="," close=")">
#{rolelist}
</foreach>
</select>
<!--Map方式入参-->
<select id="getUserByConditionMap_foreach_map" resultType="User">
/* 此处的参数gender和测试方法中的Map键值一样-conditionMap.put("gender",1);*/
select * from smbms_user where gender=#{gender} and userrole in
<foreach collection="roleIds" item="rolemap"
open ="(" separator="," close=")">
#{rolemap}
</foreach>
</select>
<!--将list封装成一个Map进行入参-->
<select id="getUser_foreach_ByMap" resultType="User">
select * from smbms_user where userrole in
<foreach collection="rolekey" item="rolemap"
open ="(" separator="," close=")">
#{rolemap}
</foreach>
</select>
<!---->
<select id="getUserList_choose" resultType="User">
select * from smbms_user where 1=1
<choose>
<when test="username!=null and username!=''">
and username like concat('%',#{username},'%')
</when>
<when test="userrole!=null and userrole!=0">
and userrole =#{userrole}
</when>
<when test="usercode!=null and usercode!=''">
and usercode like concat('%',#{usercode},'%')
</when>
<otherwise>
and creationDate=#{creationDate}
</otherwise>
</choose>
</select>
<!--查询用户表的记录数-->
<select id="count" resultType="int">
select count(1) as count from smbms_user
</select>
<!--分页查询-->
<!--<resultMap id="userRoleResult" type="User">
<id property="id" column="id"/>
<result property="username" column="username"/>
<result property="address" column="address"/>
<association property="role" javaType="Role">
<id property="id" column="id"/>
<result property="rolename" column="rolename"/>
</association>
</resultMap>-->
<!--分页查询-->
<resultMap id="roleUserResult" type="Role">
<id property="id" column="id"/>
<result property="rolename" column="rolename"/>
<collection property="userList" ofType="User">
<id property="id" column="id"/>
<result property="username" column="username"/>
<result property="address" column="address"/>
</collection>
</resultMap>
<select id="getUserListpage1" resultMap="roleUserResult">
select u.username,u.address,r.rolename from smbms_user u,smbms_role r where u.userrole=r.id
<if test="username!=null and username!=''">
and u.username like concat('%',#{username},'%')
</if>
<if test="userrole!=null and userrole!=0">
and u.userrole=#{userrole}
</if>
order by u.creationDate desc limit #{from},#{pagesize}
</select>
<select id="getUserListpage2" resultMap="roleUserResult">
select u.username,u.address,r.rolename from smbms_user u,smbms_role r where u.userrole=r.id
<if test="username!=null and username!=''">
and u.username like concat('%',#{username},'%')
</if>
<if test="userrole!=null and userrole!=0">
and u.userrole=#{userrole}
</if>
order by u.creationDate desc limit #{from},#{pagesize}
</select>
<!--在user实体类中添加rolename属性-->
<select id="getUserListpage3" resultType="User">
select u.username,u.address,r.rolename as userrolename from smbms_user u,smbms_role r where u.userrole=r.id
<if test="username!=null and username!=''">
and u.username like concat('%',#{username},'%')
</if>
<if test="userrole!=null and userrole!=0">
and u.userrole=#{userrole}
</if>
order by u.creationDate desc limit #{from},#{pagesize}
</select>
<!-- <select id="getuserlist">
select * from sys_users
<where>
<if test="username!=null and username!=''">
and user_name like CONCAT('%',#{username},'%')
</if>
<if test="">
</if>
</where>
</select>-->
</mapper>
mybatis-config核心配置文件
<?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>
<!--实体类所在包名-->
<settings>
<setting name="autoMappingBehavior" value="FULL"/>
</settings>
<typeAliases>
<package name="cn.com.yan.pojo"/>
</typeAliases>
<environments default="development">
<environment id="development">
<transactionManager type="JDBC"/>
<dataSource type="POOLED">
<property name="driver" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/supermarket?characterEncoding=UTF-8"/>
<property name="username" value="root"/>
<property name="password" value="000000"/>
</dataSource>
</environment>
</environments>
<mappers>
<!--映射配置文件下的路径-->
<mapper resource="mapper/UserMapper.xml"/>
</mappers>
</configuration>
UserMapper接口
package cn.com.yan.mapper;
import cn.com.yan.pojo.Role;
import cn.com.yan.pojo.User;
import org.apache.ibatis.annotations.Param;
import java.util.List;
import java.util.Map;
public interface UserMapper {
/**
*根据用户名和角色id查询用户信息
* @param username
* @param userrole
* @return
*/
List<User> getuserlist(@Param("username")String username,@Param("userrole")Integer userrole);
/**
* 通过id修改指定用户信息
* @param user
* @return
*/
public Integer modify(User user);
/**
* 数组的方式传参
* 根据用户角色列表。获取该角色列表下用户列表信息-foreach-array
* @param roleIds
* @return
*/
public List<User> getUserByRoleId_foreach_array(Integer[] roleIds);
/**
*list集合的方式传参
* @param rolelist
* @return
*/
public List<User> getUserByRoleId_foreach_list(List<Integer> rolelist);
/**
* Map的方式传参
* @param conditionMap
* @return
*/
public List<User> getUserByConditionMap_foreach_map(Map<String,Object> conditionMap);
/**
* 将list集合封装成Map方式作为参数
* @param roleMap
* @return
*/
public List<User> getUser_foreach_ByMap(Map<String,Object> roleMap);
/**
*根据多条件查询
* @param username
* @param userrole
* @param usercode
* @param creationDate
* @return
*/
public List<User> getUserList_choose(@Param("username")String username,
@Param("userrole")Integer userrole,
@Param("usercode")String usercode,
@Param("creationDate")String creationDate);
/**
* 查询表中数据条数
* @return
*/
public Integer count();
/**
* 分页查询
* @param username
* @param userrole
* @param currentpage
* @param pagesize
* @return
*/
public List<Role> getUserListpage1(@Param("username")String username, @Param("userrole")Integer userrole, @Param("from")Integer currentpage,
@Param("pagesize")Integer pagesize);
public List<Role> getUserListpage2(@Param("username")String username, @Param("userrole")Integer userrole, @Param("from")Integer currentpage,
@Param("pagesize")Integer pagesize);
public List<User> getUserListpage3(@Param("username")String username, @Param("userrole")Integer userrole, @Param("from")Integer currentpage,
@Param("pagesize")Integer pagesize);
}
User实体类
package cn.com.yan.pojo;
/**
* @author ysf
* @date 2019/8/30 19:53
* @用户实体类
*/
public class User {
private Integer id;
private String usercode;
//用户名称
private String username;
//密码
private String userpassword;
//性别
private Integer gender;
//生日
private String birthday;
//电话
private String phone;
//地址
private String address;
//角色id
private Integer userrole;
private String userrolename;
private Integer createby;
private String creationDate;
private Integer modifyby;
private String modifydate;
private Role role;
public Role getRole() {
return role;
}
public void setRole(Role role) {
this.role = role;
}
public String getUserrolename() {
return userrolename;
}
public void setUserrolename(String userrolename) {
this.userrolename = userrolename;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getUsercode() {
return usercode;
}
public void setUsercode(String usercode) {
this.usercode = usercode;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getUserpassword() {
return userpassword;
}
public void setUserpassword(String userpassword) {
this.userpassword = userpassword;
}
public Integer getGender() {
return gender;
}
public void setGender(Integer gender) {
this.gender = gender;
}
public String getBirthday() {
return birthday;
}
public void setBirthday(String birthday) {
this.birthday = birthday;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public Integer getUserrole() {
return userrole;
}
public void setUserrole(Integer userrole) {
this.userrole = userrole;
}
public Integer getCreateby() {
return createby;
}
public void setCreateby(Integer createby) {
this.createby = createby;
}
public String getCreationDate() {
return creationDate;
}
public void setCreationDate(String creationDate) {
this.creationDate = creationDate;
}
public Integer getModifyby() {
return modifyby;
}
public void setModifyby(Integer modifyby) {
this.modifyby = modifyby;
}
public String getModifydate() {
return modifydate;
}
public void setModifydate(String modifydate) {
this.modifydate = modifydate;
}
public User() {
}
public User(Integer id, String usercode, String username, String userpassword, Integer gender, String birthday, String phone, String address, Integer userrole, Integer createby, String creationDate, Integer modifyby, String modifydate) {
this.id = id;
this.usercode = usercode;
this.username = username;
this.userpassword = userpassword;
this.gender = gender;
this.birthday = birthday;
this.phone = phone;
this.address = address;
this.userrole = userrole;
this.createby = createby;
this.creationDate = creationDate;
this.modifyby = modifyby;
this.modifydate = modifydate;
}
@Override
public String toString() {
return "User{" +
"id=" + id +
", usercode='" + usercode + '\'' +
", username='" + username + '\'' +
", userpassword='" + userpassword + '\'' +
", gender=" + gender +
", birthday='" + birthday + '\'' +
", phone='" + phone + '\'' +
", address='" + address + '\'' +
", userrole=" + userrole +
", createby=" + createby +
", creationDate='" + creationDate + '\'' +
", modifyby=" + modifyby +
", modifydate='" + modifydate + '\'' +
'}';
}
}
Role角色实体类
package cn.com.yan.pojo;
import java.util.List;
/**
* @author ysf
* @date 2019/8/30 19:53
* @角色实体类
*/
public class Role {
private Integer id;
private String rolecode;
private String rolename;
private Integer createby;
private String creationdate;
private Integer modifyby;
private String modifydate;
List<User> userList;
public List<User> getUserList() {
return userList;
}
public void setUserList(List<User> userList) {
this.userList = userList;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getRolecode() {
return rolecode;
}
public void setRolecode(String rolecode) {
this.rolecode = rolecode;
}
public String getRolename() {
return rolename;
}
public void setRolename(String rolename) {
this.rolename = rolename;
}
public Integer getCreateby() {
return createby;
}
public void setCreateby(Integer createby) {
this.createby = createby;
}
public String getCreationdate() {
return creationdate;
}
public void setCreationdate(String creationdate) {
this.creationdate = creationdate;
}
public Integer getModifyby() {
return modifyby;
}
public void setModifyby(Integer modifyby) {
this.modifyby = modifyby;
}
public String getModifydate() {
return modifydate;
}
public void setModifydate(String modifydate) {
this.modifydate = modifydate;
}
public Role() {
}
public Role(Integer id, String rolecode, String rolename, Integer createby, String creationdate, Integer modifyby, String modifydate) {
this.id = id;
this.rolecode = rolecode;
this.rolename = rolename;
this.createby = createby;
this.creationdate = creationdate;
this.modifyby = modifyby;
this.modifydate = modifydate;
}
@Override
public String toString() {
return "Role{" +
"id=" + id +
", rolecode='" + rolecode + '\'' +
", rolename='" + rolename + '\'' +
", createby=" + createby +
", creationdate='" + creationdate + '\'' +
", modifyby=" + modifyby +
", modifydate='" + modifydate + '\'' +
'}';
}
}
工具类MybatisUtil
package cn.com.yan;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import java.io.InputStream;
/**
* @author ysf
* @date 2019/8/28 21:20
*/
public class MybatisUtil {
private static SqlSessionFactory factory;
static { //在静态代码块下factory只会被创建一次
try {
InputStream is = Resources.getResourceAsStream("mybatis-config.xml");
factory=new SqlSessionFactoryBuilder().build(is);
}catch (Exception e){
e.printStackTrace();
}
}
/**
* 创建SqlSession对象
* @return
*/
public static SqlSession createSqlSession(){
return factory.openSession(false); //true为自动提交事务
}
/**
* 关闭SqlSession对象
* @param sqlsession
*/
public static void closeSqlSession(SqlSession sqlsession){
if(sqlsession!=null){
sqlsession.close();
}
}
}
Main方法
package cn.com.yan;
import cn.com.yan.mapper.UserMapper;
import cn.com.yan.pojo.Role;
import cn.com.yan.pojo.User;
import org.apache.ibatis.session.SqlSession;
import org.junit.Test;
import java.sql.Date;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.logging.Logger;
public class Main {
private static final Logger LOGGER = Logger.getLogger(String.valueOf(Main.class));
public static void main(String[] args) {
// write your code here
}
/**
* 根据用户名和角色Id查询用户的信息
*/
@Test
public void testgetuserlist() {
SqlSession sqlsession = null;
List<User> list = new ArrayList<User>();
try {
sqlsession = MybatisUtil.createSqlSession();
User user = new User();
/*user.setUsername("小");
user.setUserrole(2);*/
String username = "明";
Integer roleId = 0;
list = sqlsession.getMapper(UserMapper.class).getuserlist(username, roleId);
for (User users : list) {
System.out.println(users.getUsername() + "\t" + users.getAddress());
}
} catch (Exception e) {
e.printStackTrace();
} finally {
MybatisUtil.closeSqlSession(sqlsession);
}
}
/**
*根据用户id修改用户信息
*/
@Test
public void testmodify() {
SqlSession sqlsession = null;
int count = 0;
try {
sqlsession = MybatisUtil.createSqlSession();
User user = new User();
user.setId(3);
user.setUsername("小明");
user.setAddress("上海浦东区");
count = sqlsession.getMapper(UserMapper.class).modify(user);
//提交事务
sqlsession.commit();
} catch (Exception e) {
e.printStackTrace();
sqlsession.rollback();
count = 0;
} finally {
MybatisUtil.closeSqlSession(sqlsession);
}
}
/**
* 数组的方式传参
* 根据用户角色列表,获取该角色列表下用户列表信息-foreach-array
*/
@Test
public void getUserByRoleId_foreach_array() {
SqlSession sqlsession = null;
List<User> list = new ArrayList<User>();
Integer roleids[] = {2, 3};
try {
sqlsession=MybatisUtil.createSqlSession();
list=sqlsession.getMapper(UserMapper.class).getUserByRoleId_foreach_array(roleids);
} catch (Exception e) {
e.printStackTrace();
} finally {
MybatisUtil.closeSqlSession(sqlsession);
}
System.out.println("姓名\t用户角色Id\t用户地址");
for (User user:list){
System.out.println(" "+user.getUsername() + "\t" +user.getUserrole()+"\t\t\t"+ user.getAddress());
}
}
/**
* list集合的方式传参
* 根据用户角色列表查询用户信息
*/
@Test
public void getUserByRoleId_foreach_list() {
SqlSession sqlsession = null;
List<User> list = new ArrayList<User>();
List<Integer> rolelist =new ArrayList<Integer>();
rolelist.add(2);
rolelist.add(3);
try {
sqlsession=MybatisUtil.createSqlSession();
list=sqlsession.getMapper(UserMapper.class).getUserByRoleId_foreach_list(rolelist);
} catch (Exception e) {
e.printStackTrace();
} finally {
MybatisUtil.closeSqlSession(sqlsession);
}
System.out.println("姓名\t用户角色Id\t用户地址");
for (User user:list){
System.out.println(" "+user.getUsername() + "\t" +user.getUserrole()+"\t\t\t"+ user.getAddress());
}
}
/**
* Map的方式传参
* 查询符合条件的用户信息
*/
@Test
public void getUserByConditionMap_foreach_map() {
SqlSession sqlsession = null;
List<User> list = new ArrayList<User>();
Map<String,Object> conditionMap=new HashMap<String,Object>();
List<Integer> rolelist =new ArrayList<Integer>();
rolelist.add(2);
rolelist.add(3);
conditionMap.put("gender",1);
conditionMap.put("roleIds",rolelist);
try {
sqlsession=MybatisUtil.createSqlSession();
list=sqlsession.getMapper(UserMapper.class).getUserByConditionMap_foreach_map(conditionMap);
} catch (Exception e) {
e.printStackTrace();
} finally {
MybatisUtil.closeSqlSession(sqlsession);
}
System.out.println("姓名\t用户角色Id\t用户地址\t性别");
for (User user:list){
System.out.println(" "+user.getUsername() + "\t" +user.getUserrole()+"\t\t\t"+ user.getAddress()+"\t"+user.getGender());
}
}
/**
*将list集合封装成Map方式作为参数,查询用户信息
*/
@Test
public void getUser_foreach_ByMap() {
SqlSession sqlsession = null;
List<User> list = new ArrayList<User>();
Map<String,Object> roleMap=new HashMap<String,Object>();
List<Integer> rolelist =new ArrayList<Integer>();
rolelist.add(2);
rolelist.add(3);
roleMap.put("rolekey",rolelist);
try {
sqlsession=MybatisUtil.createSqlSession();
list=sqlsession.getMapper(UserMapper.class).getUser_foreach_ByMap(roleMap);
} catch (Exception e) {
e.printStackTrace();
} finally {
MybatisUtil.closeSqlSession(sqlsession);
}
System.out.println("姓名\t用户角色Id\t用户地址\t性别");
for (User user:list){
System.out.println(" "+user.getUsername() + "\t" +user.getUserrole()+"\t\t\t"+ user.getAddress()+"\t"+user.getGender());
}
}
/**
* 根据多条件查询
* @throws ParseException
*/
@Test
public void getUserList_choose() throws ParseException {
SqlSession sqlsession = null;
List<User> list = new ArrayList<User>();
String username="";
Integer userrole=2;
String usercode="";
//Date creationDate= (Date) new SimpleDateFormat("YYYY-MM-dd").parse("2018-2-10");
String creationdate="2018-2-10";
try {
sqlsession=MybatisUtil.createSqlSession();
list=sqlsession.getMapper(UserMapper.class).getUserList_choose(username,userrole,usercode,creationdate);
} catch (Exception e) {
e.printStackTrace();
} finally {
MybatisUtil.closeSqlSession(sqlsession);
}
System.out.println("姓名\t用户角色Id\t用户地址\t性别");
for (User user:list){
System.out.println(" "+user.getUsername() + "\t" +user.getUserrole()+"\t\t\t"+ user.getAddress()+"\t"+user.getGender());
}
}
/**
* 分页查询
* 以下共有三个list1,list2,list3分别代表不同的方法实现分页查询
*/
@Test
public void getUserListpage() {
SqlSession sqlsession = null;
List<Role> list1=new ArrayList<Role>();
//List<Role> list2 = new ArrayList<Role>();
//List<User> list3 = new ArrayList<User>();
String username="";
Integer userrole=2;
Integer pagsize=3;
Integer currentpageNo=0;
try {
sqlsession=MybatisUtil.createSqlSession();
list1=sqlsession.getMapper(UserMapper.class).getUserListpage1(username,userrole,currentpageNo,pagsize);
//list2=sqlsession.getMapper(UserMapper.class).getUserListpage2(username,userrole,currentpageNo,pagsize);
//list3=sqlsession.getMapper(UserMapper.class).getUserListpage3(username,userrole,currentpageNo,pagsize);
} catch (Exception e) {
e.printStackTrace();
} finally {
MybatisUtil.closeSqlSession(sqlsession);
}
System.out.println("姓名\t用户地址\t\t角色名称");
//方法一
List<Role> userList1=list1;
for(Role role:userList1){
for(int i=0;i<role.getUserList().size();i++){
System.out.println(role.getUserList().get(i).getUsername()+"\t"+role.getUserList().get(i).getAddress()+"\t"+role.getRolename());
}
}
//方法二
/*List<User> userList2 = list2.get(0).getUserList();
for (User user:userList2){
System.out.println(" "+user.getUsername() + "\t"+user.getAddress()+"\t"+list2.get(0).getRolename());
}*/
//方法三
/*List<User> userList3=list3;
for(User user:userList3){
System.out.println(" "+user.getUsername() + "\t"+user.getAddress()+"\t"+user.getUserrolename());
}*/
}
}
数据库就不展示了,有看不懂的可以留言,当然有不对的地方欢迎指正。
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。