public interface UserMapper {
// 表示查询总记录数
public int count() throws Exception;
// 表示查询所有数据
public List<User> findAll() throws Exception;
public List<User> getUserList(User user) throws Exception;
public List<User> getUserListb(User user) throws Exception;
public List<User> getuserListMap(Map<String, String> userMap)
throws Exception;
public List<User> getuserListResult(User user) throws Exception;
public int insertMethod(User user) throws Exception;
public int deleteMethod(User user) throws Exception;
public int deletemethod2(@Param("id1")Integer delId) throws Exception;
public int updateUserName(@Param("id")Integer id,@Param("userName")String username) throws Exception;
public List<User> getUserByID(@Param("userRole")Integer id) throws Exception;
public List<User> getAddressListById(@Param("id")Integer userId) throws Exception;
public List<User> getUserListByNameById(@Param("userName")String userName,@Param("userRole")Integer roleId) throws Exception;
public List<User> getUserByRoleId_foreach_array(List<Integer> roleIds) throws Exception;
public List<User> getUserByMap_foreach_map(Map<String,Object> map) throws Exception;
public List<User> getUserList_choose(@Param("userName")String userName,
@Param("userRole")Integer roleId,
@Param("userCode")String userCode,
@Param("creationDate")Date creationDate
) throws Exception;
}
<?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.cn.mapper.UserMapper">
<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="userCode != null and userCode != ''">
and userCode like concat('%',#{userCode},'%')
</when>
<when test="userRole != null">
and userRole=#{userRole}
</when>
<otherwise>
<!-- 前面creationDate表中的字段名称 后面#{creationDate}表示入参进来的数据 -->
and YEAR(creationDate) = YEAR(#{creationDate})
</otherwise>
</choose>
</select>
<select id="getUserByMap_foreach_map" resultMap="userMapByRole">
select * from smbms_user where gender=1 and userRole in
<foreach collection="rkey" item="roleMap" open="(" separator="," close=")">
#{roleMap}
</foreach>
</select>
<resultMap id="userMapByRole" type="user">
<id property="id" column="id"/>
<result property="userCode" column="userCode"/>
<result property="userName" column="userName"/>
</resultMap>
<select id="getUserByRoleId_foreach_array" resultMap="userMapByRole">
<!--
collection:
1、如果入参为单擦单参数 并且是数组的时候 collection的值为array
2、如果入参为单擦单参数 并且是List的时候 collection的值为list
3、如果为多参数 可封装为Map
item:
表示集合中每一个元素进行迭代时候的名称
open:
表示该语句以什么开始
separator:
表示每次进行迭代之前以什么符号进行分隔
close:
表示该语句以什么结束
-->
select * from smbms_user where userRole in
<foreach collection="list" item="roleIds" open="(" separator="," close=")" >
#{roleIds}
</foreach>
</select>
<resultMap id="getUserMapSJ" type="user">
<id property="id" column="id" />
<result property="userCode" column="userCode" />
<result property="userName" column="userName" />
<result property="phone" column="phone" />
<result property="birthday" column="birthday" />
<result property="userRole" column="userRole" />
<result property="userRoleName" column="roleName" />
</resultMap>
<!-- resultMap="getUserMapSJ" -->
<select id="getUserListByNameById" resultType="user">
<!-- select u.*,r.roleName from smbms_user u ,smbms_role r where u.userRole
= r.id <if test="userRole!=null"> and u.userRole = #{userRole} </if> <if
test="userName!=null and userName!=''"> and u.userName like concat('%',#{userName},'%')
</if> -->
select * from smbms_user
<!-- 推荐使用trim -->
<trim prefix="where" prefixOverrides="and|or">
<if test="userName!=null and userName!=''">
userName like concat('%',#{userName},'%')
</if>
<if test="userRole!=null">
and userRole = #{userRole}
</if>
</trim>
<!-- where表示标签 自动识别标签内是否有返回值 如果返回的值中开头有and或者or则自动删除 -->
<!-- <where> <if test="userName!=null and userName!=''"> userName like
concat('%',#{userName},'%') </if> <if test="userRole!=null"> and userRole
= #{userRole} </if> </where> -->
</select>
<resultMap id="getAddressMap" type="user">
<id property="id" column="id" />
<result property="userCode" column="userCode" />
<result property="userName" column="userName" />
<!-- ofType="类的完全限定名" com.bdqn.cn.user.Address -->
<!-- association和collection 不能在一起连用:association为一对一 collection一对多 区别:association将数据映射到单个对象当中
collection将数据映射到集合当中 -->
<collection property="addressList" ofType="address">
<id property="id" column="id" />
<result property="postCode" column="postCode" />
<result property="tel" column="tel" />
<result property="contact" column="contact" />
<result property="addressDesc" column="addressDesc" />
</collection>
</resultMap>
<select id="getAddressListById" parameterType="Integer"
resultMap="getAddressMap">
select u.*,r.id,r.contact,r.addressDesc,r.postCode,r.tel from smbms_user
u,smbms_address r
where u.id = r.userId and u.id=#{id}
</select>
<cache eviction="FIFO" flushInterval="60000" size="512" readOnly="true" />
<!-- <resultMap id="userRoleMap" type="user"> <id property="id" column="id"/>
<result property="userCode" column="userCode"/> <result property="userName"
column="userName"/> <result property="userRole" column="userRole"/> <association
property="role" javaType="role"> <id property="id" column="id"/> <result
property="roleCode" column="roleCode"/> <result property="roleName" column="roleName"/>
</association> </resultMap> -->
<resultMap id="userRoleMap" type="user">
<!-- 这里的id 一般用数据库表中的主键 用result也是没有错的 但是会降低mybatis的性能 -->
<id property="id" column="id" />
<result property="userCode" column="userCode" />
<result property="userName" column="userName" />
<result property="userRole" column="userRole" />
<association property="role" javaType="role" resultMap="resultMapselect" />
</resultMap>
<resultMap id="resultMapselect" type="role">
<id property="id" column="id" />
<result property="roleCode" column="roleCode" />
<result property="roleName" column="roleName" />
</resultMap>
<select id="getUserByID" parameterType="Integer" resultMap="userRoleMap">
select u.*,r.id,r.roleCode,r.roleName from smbms_user u,smbms_role
r where u.userRole = #{userRole} and r.id = u.userRole
</select>
<update id="updateUserName">
<!-- 第一种方式 -->
<!-- update smbms_user set userName=#{userName} where id=#{id} -->
<!-- 第二种方式 -->
<!-- update smbms_user -->
<!-- SQL语句标签化后可以剔除后面多余"," -->
<!-- <set> <if test="userName!=null">userName=#{userName},</if> <if test="userName!=null">userName=#{userName},</if>
<if test="userName!=null">userName=#{userName},</if> <if test="userName!=null">userName=#{userName},</if>
<if test="userName!=null">userName=#{userName},</if> </set> where id=#{id} -->
<!-- 第三种方式 trim可以设置前缀和后缀 和 select中的使用方式一样 -->
update smbms_user
<trim prefix="set" suffixOverrides="," suffix="where id=#{id}">
<if test="userName!=null">userName=#{userName},</if>
</trim>
</update>
<delete id="deletemethod2" parameterType="Integer">
delete from smbms_user where id=#{id1}
</delete>
<delete id="deleteMethod" parameterType="int">
delete from smbms_user where id=#{id}
</delete>
<insert id="insertMethod" parameterType="user">
insert into smbms_user(userCode,userName,userPassword)
VALUES(#{userCode},#{userName},#{userPassword})
</insert>
<!-- 使用resultMap进行 -->
<resultMap type="user" id="userList">
<result property="id" column="id" />
<result property="userCode" column="userCode" />
<result property="userName" column="userName" />
<result property="phone" column="phone" />
<result property="birthday" column="birthday" />
<!-- <result property="gender" column="gender"/> -->
<result property="userRole" column="userRole" />
<result property="userRoleName" column="roleName" />
</resultMap>
<!-- 使用resultMap进行映射 paramterType入参 resultType返回值 -->
<select id="getuserListResult" parameterType="user" resultMap="userList">
<!-- #{userName} 多参数入参 -->
select u.*,r.roleName from smbms_user u,smbms_role r where u.userName
like CONCAT('%',#{userName},'%') and u.userRole = #{userRole} and
u.userRole = r.id
</select>
<!-- 使用Map集合进行入参 paramterType入参 resultType返回值 -->
<select id="getuserListMap" parameterType="Map" resultType="user">
<!-- #{userName} 多参数入参 -->
select * from smbms_user where userName like
CONCAT('%',#{userName},'%') and userRole=#{userRole}
</select>
<!-- 以下语句进行模糊查询 多个参数进行入参 paramterType入参 resultType返回值 -->
<select id="getUserListb" parameterType="user" resultType="user">
<!-- #{userName} 多参数入参 -->
select * from smbms_user where userName like
CONCAT('%',#{userName},'%') and userRole=#{userRole}
</select>
<!-- 以下语句进行模糊查询 单个参数进行入参 paramterType入参 resultType返回值 -->
<select id="getUserList" parameterType="user" resultType="user">
<!-- #{userName} -->
select * from smbms_user where userName like
CONCAT('%',#{userName},'%')
</select>
<!-- com.bdqn.cn.user.User resultType返回值类型 -->
<select id="count" resultType="int">
<!-- 编写sql语句 -->
select count(*) from smbms_user
</select>
<select id="findAll" resultType="user">
<!-- 编写sql语句 -->
select * from smbms_user
</select>
</mapper>
public class Address {
private Integer id;
private String postCode;
private String addressDesc;
private String contact;
private String tel;
private Integer createBy;
private Date creationDate;
private Integer modifyBy;
private Date modifyDate;
private Integer userId;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getPostCode() {
return postCode;
}
public void setPostCode(String postCode) {
this.postCode = postCode;
}
public String getAddressDesc() {
return addressDesc;
}
public void setAddressDesc(String addressDesc) {
this.addressDesc = addressDesc;
}
public String getContact() {
return contact;
}
public void setContact(String contact) {
this.contact = contact;
}
public String getTel() {
return tel;
}
public void setTel(String tel) {
this.tel = tel;
}
public Integer getCreateBy() {
return createBy;
}
public void setCreateBy(Integer createBy) {
this.createBy = createBy;
}
public Date getCreationDate() {
return creationDate;
}
public void setCreationDate(Date creationDate) {
this.creationDate = creationDate;
}
public Integer getModifyBy() {
return modifyBy;
}
public void setModifyBy(Integer modifyBy) {
this.modifyBy = modifyBy;
}
public Date getModifyDate() {
return modifyDate;
}
public void setModifyDate(Date modifyDate) {
this.modifyDate = modifyDate;
}
public Integer getUserId() {
return userId;
}
public void setUserId(Integer userId) {
this.userId = userId;
}
}
public class Role {
private Integer id;
private String roleCode;
private String roleName;
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 class User {
// id
private Integer id;
// 用户编码
private String userCode;
// 用户账号
private String userName;
// 用户密码
private String userPassword;
// 出生日期
private Date birthday;
// 电话
private String phone;
// 地址
private String address;
// 用户角色
private String userRole;
// 创建者
private Integer createBy;
// 创建时间
private Date creationDate;
// 更新者
private Integer modifyBy;
// 更新时间
private Date modifyDate;
private String userRoleName;
private Role role;
private List<Address> addressList;
public List<Address> getAddressList() {
return addressList;
}
public void setAddressList(List<Address> addressList) {
this.addressList = addressList;
}
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 User() {
super();
// TODO Auto-generated constructor stub
}
public User(Integer id, String userCode, String userName,
String userPassword, Date birthday, String phone, String address,
String userRole, Integer createBy, Date creationDate,
Integer modifyBy, Date modifyDate) {
super();
this.id = id;
this.userCode = userCode;
this.userName = userName;
this.userPassword = userPassword;
this.birthday = birthday;
this.phone = phone;
this.address = address;
this.userRole = userRole;
this.createBy = createBy;
this.creationDate = creationDate;
this.modifyBy = modifyBy;
this.modifyDate = modifyDate;
}
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 Date getBirthday() {
return birthday;
}
public void setBirthday(Date 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 String getUserRole() {
return userRole;
}
public void setUserRole(String userRole) {
this.userRole = userRole;
}
public Integer getCreateBy() {
return createBy;
}
public void setCreateBy(Integer createBy) {
this.createBy = createBy;
}
public Date getCreationDate() {
return creationDate;
}
public void setCreationDate(Date creationDate) {
this.creationDate = creationDate;
}
public Integer getModifyBy() {
return modifyBy;
}
public void setModifyBy(Integer modifyBy) {
this.modifyBy = modifyBy;
}
public Date getModifyDate() {
return modifyDate;
}
public void setModifyDate(Date modifyDate) {
this.modifyDate = modifyDate;
}
}
public class MybatisUtil {
private static SqlSessionFactory factory;
// 静态代码块
static{
try {
InputStream is = Resources.getResourceAsStream("mybatis-config.xml");
factory = new SqlSessionFactoryBuilder().build(is);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static SqlSession createSqlSession(){
// factory.openSession()相当于返回了一个sqlsession对象
return factory.openSession(false); // 表示事物提交 false表示不自动提交 true表示自动提交
}
public static void closeSqlSession(SqlSession sqlSession){
if(sqlSession != null){
sqlSession.close();
}
}
}
配置文件中的信息
driver=com.mysql.jdbc.Driver
url=jdbc:mysql://?zeroDateTimeBehavior=convertToNull
username=root
password=123456
<?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="driver" value="com.mysql.jdbc.Driver"
/> <property name="url" value="jdbc:mysql:///smbms?zeroDateTimeBehavior=convertToNull"
/> <property name="username" value="root" /> <property name="password" value="123.com"
/> </properties> -->
<properties resource="databases.properties">
</properties>
<settings>
<!-- 自动映射级别 -->
<setting name="autoMappingBehavior" value="FULL" />
<setting name="cacheEnabled" value="true" />
<setting name="logImpl" value="log4j" />
</settings>
<typeAliases>
<!-- 单个设置别名 -->
<!-- <typeAlias alias="user" type="com.cn.user.User" /> <typeAlias
alias="role" type="com.bdqn.cn.user.Role" /> -->
<!-- 批量设置别名 -->
<package name="com.bdqn.cn.user" />
</typeAliases>
<plugins>
<plugin interceptor="com.github.pagehelper.PageHelper">
<!-- 设置数据库类型 -->
<property name="dialect" value="mysql"></property>
</plugin>
</plugins>
<environments default="development">
<environment id="development">
<transactionManager type="JDBC" />
<dataSource type="POOLED">
<property name="driver" value="${driver}" />
<property name="url" value="${url}" />
<property name="username" value="${username}" />
<property name="password" value="${password}" />
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource="com/cn/mapper/UserMappper.xml" />
</mappers>
</configuration>