public interface UserMapper {
// represents the total number of records the query
public int count () throws Exception;
// indicates that the query data for all
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"/>
<Property Result = "userCode" column = "userCode" />
<Property Result = "the userName" column = "the userName" />
</ The resultMap>
<SELECT ID = "getUserByRoleId_foreach_array" The resultMap = "userMapByRole">
<-!
Collection :
1, if the parameter is a single parameter and a single wipe when the collection array value array
2, if the parameter is a single parameter and a single rub List collection time value List
. 3, if the package is a multi-parameter the Map
Item:
represent each iteration when the name of the elements of the collection
open:
represents the beginning of what the statement
separator:
indicate what symbol to separate them before each iterative
close:
Indicates that the statement to what end
->
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 the return value indicates whether there is an automatic tag identification tag if there is the beginning and the returned value is automatically deleted or or ->
<-! <WHERE> <= IF Test "and the userName = null the userName!! = '' "> the userName like
the concat ( '%', # {the userName}, '%') </ IF> <IF Test =" userRole! = null "> and userRole
= # {userRole} </ IF> </ where> ->
</select>
<resultMap id="getAddressMap" type="user">
<Property ID = "ID" column = "ID" />
<Property Result = "userCode" column = "userCode" />
<Property Result = "the userName" column = "the userName" />
<-! ofType = "Class the fully qualified name "com.bdqn.cn.user.Address ->
<- Association and collection can not be used in conjunction with:! association is one-to-many collection distinguished: association mapping the data to a single object which
collection will map data to set them ->
<= collection Property "as addressList" ofType = "address">
<Property ID = "ID" column = "ID" />
<Property Result = "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} -->
<! - the third embodiment may be provided trim use and select prefixes and suffixes in the same ->
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"/> <SELECT ID = "getuserListResult" the parameterType = "User" resultMap = "userList"> <-! ResultMap using the reference map paramterType resultType return value ->
</ ResultMap>
<-! # {UserName} multiple parameters into parameters ->
SELECT * U, U r.roleName from smbms_user, R & lt smbms_role WHERE u.userName.
Like CONCAT ( '%', the userName # {}, '%') and {#} = userRole u.userRole and
u.userRole r.id =
</ SELECT>
<-! Map using the set of parameters into the reference paramterType resultType return value ->
<SELECT ID = "getuserListMap" the parameterType = "Map "the resultType =" User ">
<-! # {} multiparameter parameters into the userName ->
SELECT * WHERE smbms_user from the userName like
CONCAT ( '%', the userName # {}, '%') = # {userRole and userRole }
</ SELECT>
<-! following statement fuzzy query parameters into a plurality of parameters into the reference paramterType resultType return value ->
<SELECT ID = "getUserListb" parameterType="user" resultType="user">
<-! # {UserName} multiple parameters into parameters ->
SELECT * WHERE smbms_user from the userName like
CONCAT ( '%', the userName # {}, '%') and userRole userRole} = {#
</ SELECT>
<! - the following statement fuzzy query parameters into a single parameter paramterType resultType return value into parameter ->
<SELECT ID = "getUserList" the 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;
}
}
the User class {public
// the above mentioned id
Private Integer the above mentioned id;
// user code
Private String userCode;
// user account
Private String userName;
// user password
Private String the userPassword;
// date of birth
Private Birthday a Date;
// telephone
private String phone;
// address
Private String address;
// user roles
Private String userRole;
// creator
Private Integer createBy;
// create time
Private creationDate a Date;
// updated by
Private Integer modifyBy;
// update
Private ModifyDate a Date;
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;
}
}
{class MybatisUtil public
Private a SqlSessionFactory static Factory;
// static block
static {
the try {
the InputStream Resources.getResourceAsStream IS = ( "MyBatis-the config.xml");
Factory the SqlSessionFactoryBuilder new new = () Build (IS);.
} the catch (IOException E) {
// the TODO Auto-Generated Block the catch
e.printStackTrace ();
}
}
public static createSqlSession the sqlSession () {
// factory.openSession () returns a sqlsession equivalent objects
return factory.openSession (false); // represents being filed false otherwise automatically displayed automatically submit to submit true
}
public static void closeSqlSession (the SqlSession SQLSESSION) {
if(sqlSession != null){
sqlSession.close();
}
}
}
Configuration file
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" /> -->
<! - Batch Settings Alias ->
<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>