代码实现:
package com.openlab.pojo;
import java.util.Date;
public class Address {
private int id;
private String contact;
private String addressInfo;
private String postCode;
private String tel;
private int createdBy;
private Date creationDate;
private int modifyBy;
private Date modifyDate;
private int userId;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getContact() {
return contact;
}
public void setContact(String contact) {
this.contact = contact;
}
public String getAddressInfo() {
return addressInfo;
}
public void setAddressInfo(String addressInfo) {
this.addressInfo = addressInfo;
}
public String getPostCode() {
return postCode;
}
public void setPostCode(String postCode) {
this.postCode = postCode;
}
public String getTel() {
return tel;
}
public void setTel(String tel) {
this.tel = tel;
}
public int getCreatedBy() {
return createdBy;
}
public void setCreatedBy(int createdBy) {
this.createdBy = createdBy;
}
public Date getCreationDate() {
return creationDate;
}
public void setCreationDate(Date creationDate) {
this.creationDate = creationDate;
}
public int getModifyBy() {
return modifyBy;
}
public void setModifyBy(int modifyBy) {
this.modifyBy = modifyBy;
}
public Date getModifyDate() {
return modifyDate;
}
public void setModifyDate(Date modifyDate) {
this.modifyDate = modifyDate;
}
public int getUserId() {
return userId;
}
public void setUserId(int userId) {
this.userId = userId;
}
}
package com.openlab.pojo;
import java.sql.Date;
public class Provider {
private int id;
private String proCode;
private String proName;
private String proDesc;
private String proContact;
private String proPhone;
private String proAddress;
private String proFax;
private int createBy;
private Date creationDate;
private Date modifyDate;
private int modifyBy;
private String companyLicPicPath;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getProCode() {
return proCode;
}
public void setProCode(String proCode) {
this.proCode = proCode;
}
public String getProName() {
return proName;
}
public void setProName(String proName) {
this.proName = proName;
}
public String getProDesc() {
return proDesc;
}
public void setProDesc(String proDesc) {
this.proDesc = proDesc;
}
public String getProContact() {
return proContact;
}
public void setProContact(String proContact) {
this.proContact = proContact;
}
public String getProPhone() {
return proPhone;
}
public void setProPhone(String proPhone) {
this.proPhone = proPhone;
}
public String getProAddress() {
return proAddress;
}
public void setProAddress(String proAddress) {
this.proAddress = proAddress;
}
public String getProFax() {
return proFax;
}
public void setProFax(String proFax) {
this.proFax = proFax;
}
public int getCreateBy() {
return createBy;
}
public void setCreateBy(int createBy) {
this.createBy = createBy;
}
public Date getCreationDate() {
return creationDate;
}
public void setCreationDate(Date creationDate) {
this.creationDate = creationDate;
}
public Date getModifyDate() {
return modifyDate;
}
public void setModifyDate(Date modifyDate) {
this.modifyDate = modifyDate;
}
public int getModifyBy() {
return modifyBy;
}
public void setModifyBy(int modifyBy) {
this.modifyBy = modifyBy;
}
public String getCompanyLicPicPath() {
return companyLicPicPath;
}
public void setCompanyLicPicPath(String companyLicPicPath) {
this.companyLicPicPath = companyLicPicPath;
}
public String getOrgCodePicPath() {
return orgCodePicPath;
}
public void setOrgCodePicPath(String orgCodePicPath) {
this.orgCodePicPath = orgCodePicPath;
}
private String orgCodePicPath;
}
package com.openlab.pojo;
import java.util.Date;
public class Role {
private int id;
private String roleCode;
private String roleName;
private int createdBy;
private Date creationDate;
private int modifyBy;
private Date modifyDate;
public int getId() {
return id;
}
public void setId(int 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 int getCreatedBy() {
return createdBy;
}
public void setCreatedBy(int createdBy) {
this.createdBy = createdBy;
}
public Date getCreationDate() {
return creationDate;
}
public void setCreationDate(Date creationDate) {
this.creationDate = creationDate;
}
public int getModifyBy() {
return modifyBy;
}
public void setModifyBy(int modifyBy) {
this.modifyBy = modifyBy;
}
public Date getModifyDate() {
return modifyDate;
}
public void setModifyDate(Date modifyDate) {
this.modifyDate = modifyDate;
}
}
package com.openlab.pojo;
import java.io.Serializable;
import java.util.Date;
import java.util.List;
public class User implements Serializable{
private int id;
private String userCode;
private String userName;
private String userPassword;
private int gender;
private Date birthday;
private String phone;
private String address;
private int userRole;
private int createdBy;
private Date creationDate;
private int modifyBy;
private Date modifyDate;
private String idPicPath;
private String workPicPath;
public Address address1;
public String roleName; //角色名称
public Role role;
public List<Address> addresses;
public List<Address> getAddresses() {
return addresses;
}
public void setAddresses(List<Address> addresses) {
this.addresses = addresses;
}
public Role getRole() {
return role;
}
public void setRole(Role role) {
this.role = role;
}
public String getRoleName() {
return roleName;
}
public void setRoleName(String roleName) {
this.roleName = roleName;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
public int getId() {
return id;
}
public void setId(int 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 int getGender() {
return gender;
}
public void setGender(int gender) {
this.gender = gender;
}
public Date getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public int getUserRole() {
return userRole;
}
public void setUserRole(int userRole) {
this.userRole = userRole;
}
public int getCreatedBy() {
return createdBy;
}
public void setCreatedBy(int createdBy) {
this.createdBy = createdBy;
}
public Date getCreationDate() {
return creationDate;
}
public void setCreationDate(Date creationDate) {
this.creationDate = creationDate;
}
public int getModifyBy() {
return modifyBy;
}
public void setModifyBy(int modifyBy) {
this.modifyBy = modifyBy;
}
public Date getModifyDate() {
return modifyDate;
}
public void setModifyDate(Date modifyDate) {
this.modifyDate = modifyDate;
}
public String getIdPicPath() {
return idPicPath;
}
public void setIdPicPath(String idPicPath) {
this.idPicPath = idPicPath;
}
public String getWorkPicPath() {
return workPicPath;
}
public void setWorkPicPath(String workPicPath) {
this.workPicPath = workPicPath;
}
}
package com.openlab.dao;
public interface ProviderMapper {
//查询统计供应商的数量
public int count();
}
<?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.openlab.dao.ProviderMapper">
<!-- 查询统计用户的个数 -->
<select id="count" resultType="Integer">
select count(1) as count from smbms_provider
</select>
</mapper>
package com.openlab.dao;
import java.util.List;
import java.util.Map;
import org.apache.ibatis.annotations.Param;
import com.openlab.pojo.Address;
import com.openlab.pojo.Role;
import com.openlab.pojo.User;
public interface UserMapper {
//查询统计用户表的记录数
public int count();
//查询用户表所有的记录
public List<User> getUserList();
//根据指定的用户名进行查询
public List<User> findUserByUserName(@Param("_userName")String _userName);
//根据指定的用户对象进行查询
public List<User> findUserByUser(User _user);
//Map作为参数
public User findUserByMap(Map map);
//查询所有的地址
public List<Address> findAddressAll();
//添加地址
public int add(Address _address);
//查询指定用户的信息,包含角色名称
public User getUserInfoById(String _userId);
//修改指定用户的信息
public int updateUser(@Param("_uid")String _uid, @Param("user")User _user);
//根据主键id删除指定用户编号的信息
public int deleteUser(String id);
//查询指定角色id的用户信息
public List<User> getUserListByRoleId(String roleId);
//查询指定Id用户的所有地址信息
public List<User> getAddressListByUserId(@Param("uid")String user_id);
//添加用户
public int saveUser(User _user);
public List<User> getUserListByRoleList(@Param("list")List list);
}
<?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.openlab.dao.UserMapper">
<!-- 查询统计用户的个数 -->
<select id="count" resultType="Integer">
select count(1) as count from smbms_user
</select>
<!-- 查询所有的用户 -->
<select id="getUserList" resultType="User">
<!-- select * from smbms_user -->
select id,userCode,userName,userPassword ,address,phone from smbms_user
</select>
<!--查询指定用户名字的人的信息 -->
<select id="findUserByUserName" resultType="User" parameterType="String" >
<!-- select * from smbms_user where userName like concat('%',#{
_userName},'%') -->
<!-- <if test="_userName!=null and _userName!=''">
where userName like concat('%',#{
_userName},'%')
</if> -->
select * from smbms_user
<!-- <where>
<if test="_userName!=null and _userName!=''">
userName like concat('%',#{
_userName},'%')
</if>
</where> -->
<trim prefix="where" prefixOverrides="and|or">
<if test="_userName!=null and _userName!=''">
and userName like concat('%',#{
_userName},'%')
</if>
</trim>
</select>
<!-- 根据对象进行查询 -->
<select id="findUserByUser" resultType="User" parameterType="User">
select * from smbms_user where userName=#{
userName}
</select>
<!-- Map作为对象参数进行入参查询 -->
<select id="findUserByMap" resultType="User" parameterType="Map">
select * from smbms_user where userName like concat('%',#{
uName},'%') and gender=#{
gender1}
</select>
<select id="findAddressAll" resultType="Address" resultMap="addressDesc">
select * from smbms_address
</select>
<!-- 自定义映射 -->
<resultMap type="Address" id="addressDesc">
<result property="addressInfo" column="addressDesc"/>
</resultMap>
<!-- 简单添加操作 -->
<insert id="add" parameterType="Address" >
insert into smbms_address(id,contact,addressDesc)values(#{
id},#{
contact},#{
addressInfo})
</insert>
<!-- 关联查询 -->
<select id="getUserInfoById" parameterType="String" resultMap="userResultMap">
select u.*,r.id as rid,r.roleName from smbms_user u,smbms_role r where u.id=#{
_userId} and u.userRole=r.id
</select>
<!--定义结果集的映射 -->
<resultMap type="User" id="userResultMap">
<id column="id" property="id"></id>
<result column="rid" property="userRole"/>
<result column="roleName" property="roleName"/>
</resultMap>
<!--修改用户的信息 -->
<update id="updateUser" >
<!-- update smbms_user set
userCode=#{
user.userCode},userName=#{
user.userName},userPassword=#{
user.userPassword}
where id=#{
_uid} -->
<!-- update smbms_user set -->
<!--
<if test="user.userCode!=null">
userCode=#{
user.userCode},
</if>
<if test="user.userName!=null">
userName=#{
user.userName},
</if>
<if test="user.userPassword!=null">
userPassword=#{
user.userPassword}
</if>
<where>
<if test="_uid!=null">
id=#{
_uid}
</if>
</where> -->
update smbms_user
<trim prefix="set" suffix="where" suffixOverrides=",">
<if test="user.userCode!=null">
userCode=#{
user.userCode},
</if>
<if test="user.userName!=null">
userName=#{
user.userName},
</if>
<if test="user.userPassword!=null">
userPassword=#{
user.userPassword},
</if>
</trim>
id=#{
_uid}
</update>
<!--删除指定用户id的信息 -->
<delete id="deleteUser" parameterType="String">
delete from smbms_user where id=#{
id}
</delete>
<!--查询指定角色的用户信息 -->
<select id="getUserListByRoleId" parameterType="String" resultMap="userResultMapByRoleId" >
select u.*,r.id as r_id ,r.roleCode,r.roleName from smbms_user u,smbms_role r where u.userRole=#{
roleId} and u.userRole=r.id
</select>
<!-- 定义自定义结果集 -->
<resultMap type="User" id="userResultMapByRoleId">
<id property="id" column="id"/>
<result property="userName" column="userName"/>
<association property="role" javaType="Role" resultMap="roleList">
</association>
</resultMap>
<!-- -->
<resultMap type="Role" id="roleList">
<id property="id" column="r_id"/>
<result property="roleCode" column="roleCode"/>
<result property="roleName" column="roleName"/>
</resultMap>
<!-- 根据用户的Id查询该用户对应的所有地址 -->
<select id="getAddressListByUserId" parameterType="String" resultMap="useraddressList" useCache="true" flushCache="true">
select u.*,a.id as a_id ,a.contact,a.addressDesc,a.postCode from smbms_user u,smbms_address a where u.id=#{
uid} and u.id=a.userId
</select>
<resultMap type="User" id="useraddressList">
<id property="id" column="id"/>
<result property="userCode" column="userCode"/>
<result property="userName" column="userName"/>
<collection property="addresses" ofType="Address" resultMap="addressList">
</collection>
</resultMap>
<resultMap type="Address" id="addressList">
<id property="id" column="a_id"/>
<result property="contact" column="contact"/>
<result property="addressInfo" column="addressDesc"/>
<!-- <result property="postCode" column="postCode"/> -->
</resultMap>
<!-- 添加用户 -->
<insert id="saveUser" parameterType="User">
insert into smbms_user(userCode,userName)
<trim prefix="values(" suffix=")" suffixOverrides=",">
<if test="userCode!=null and userCode!=''">
#{
userCode},
</if>
<if test="userName!=null and userName!=''">
#{
userName},
</if>
</trim>
</insert>
<!-- 循环 -->
<select id="getUserListByRoleList" resultType="User" parameterType="List">
<!--select * from smbms_user where userRole in (1,2,3) -->
select * from smbms_user where userRole in
<foreach collection="list" item="roleList" open="(" separator="," close=")">
#{
roleList}
</foreach>
</select>
<!-- 进行缓存的相关配置 -->
<cache eviction="FIFO" flushInterval="60000" size="512" readOnly="true"/>
</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">
<!-- 配置文件完成mybatis与数据库的连接 -->
<!-- <configuration>
配置访问数据库的基本配置
<properties resource="db.properties">
<property name="driver" value="com.sql.jdbc.Driver" />
<property name="url"
value="jdbc:mysql://127.0.0.1:3306/smbms?useUnicode=true&characterEncoding=UTF-8" />
<property name="username1" value="root" />
<property name="password1" value="root" />
</properties>
设置延迟加载
<settings>
<setting name="lazyLoadingEnabled" value="false" />
</settings>
配置类的别名
<typeAliases>
<typeAlias type="com.openlab.pojo.Provider" alias="Provider" />
</typeAliases>
配置数据访问的环境
<environments default="development">
开发环境
<environment id="development">
事务的管理
<transactionManager type="JDBC"></transactionManager>
数据源的管理
<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>
配置测试环境
<environment id="test">
<transactionManager type=""></transactionManager>
<dataSource type=""></dataSource>
</environment>
配置生产环境
<environment id="make">
<transactionManager type=""></transactionManager>
<dataSource type=""></dataSource>
</environment>
</environments>
加载映射文件
<mappers>
<mapper resource="com//openlab//dao//ProviderMapper.xml" />
</mappers>
</configuration> -->
<configuration>
<!-- 配置数据库访问的基本配置 -->
<properties resource="db.properties">
<property name="driver" value="com.mysql.jdbc.Driver" />
<property name="url"
value="jdbc:mysql://127.0.0.1:3306/smbms?useUnicode=true&characterEncoding=UTF-8" />
<property name="username" value="root" />
<property name="password" value="root" />
</properties>
<!-- 设置延迟加载 -->
<settings>
<!-- <setting name="lazyLoadingEnabled" value="false" /> -->
<!-- 配置查询时关联的属性的映射级别 -->
<!-- <setting name="autoMappingBehavior" value="FULL"/> -->
<!-- 设置开启缓存 -->
<setting name="cacheEnabled" value="true"/>
</settings>
<!--配置类的别名 -->
<typeAliases>
<!-- <typeAlias type="com.openlab.pojo.User" alias="User" />
<typeAlias type="com.openlab.pojo.Address" alias="Address1"/> -->
<package name="com.openlab.pojo"/>
</typeAliases>
<!--配置数据访问的环境 -->
<environments default="development">
<!-- 开发环境 -->
<environment id="development">
<!-- 事务的管理 -->
<transactionManager type="JDBC"></transactionManager>
<!--数据源的管理 -->
<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>
<!--配置测试环境 -->
<environment id="test">
<transactionManager type=""></transactionManager>
<dataSource type=""></dataSource>
</environment>
<!--配置生产环境 -->
<environment id="make">
<transactionManager type=""></transactionManager>
<dataSource type=""></dataSource>
</environment>
</environments>
<!--加载映射文件 -->
<mappers>
<mapper resource="com//openlab//dao//UserMapper.xml" />
<!-- <mapper url="file:///d://aa//UserMapper.xml"/> -->
</mappers>
</configuration>
driver=com.mysql.jdbc.Driver
url=jdbc\:mysql\://127.0.0.1\:3306/smbms?useUnicode\=true&characterEncoding\=UTF-8
username=root
password=root
package com.openlab.test;
import java.io.IOException;
import java.io.InputStream;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
public class Test01 {
public static void main(String[] args){
//读取配置文件
String str = "mybatis-config.xml";
SqlSession sqlSession = null;
Integer count = 0;
InputStream is;
try {
is = Resources.getResourceAsStream(str);
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);
//创建SqlSession
sqlSession = sqlSessionFactory.openSession();
//执行查询供应商的数量的方法
count = sqlSession.selectOne("com.openlab.dao.UserMapper.count");
} catch (IOException e) {
e.printStackTrace();
}
}
}
package com.openlab.test;
import java.util.List;
import org.apache.ibatis.session.SqlSession;
import com.openlab.pojo.Address;
import com.openlab.dao.UserMapper;
import com.openlab.utils.SqlSessionUtils;
public class Test02 {
public static void main(String[] args) {
SqlSession sqlSession=SqlSessionUtils.createSqlSession();
List<Address> list=sqlSession.getMapper(UserMapper.class).findAddressAll();
for(Address address:list){
System.out.println(address.getId());
System.out.println(address.getContact());
System.out.println(address.getTel());
//System.out.println(address.getAddressDesc());
System.out.println(address.getAddressInfo());
}
SqlSessionUtils.closeSqlSession(sqlSession);
}
}