2019.03.08(mybatis框架Dao和service层实现案例)

mybatis框架Dao和service层实现案例

代码编写规范

  1. 接口名必须与mapper的文件名保持一致
  2. 接口包的路径和nameSpace保持一致
  3. 接口里面的方法名与id的名字保持一致3,接口里面的方法名与id的名字保持一致
  4. 接口里面的返回值类型与ResultType(Map) 保持一致
  5. 接口里面的参数必须与ParamterType保持一致;

单表查询

Dao层

Dao接口:CustomerCategory为实体类

public interface CustomerCategoryDao {
    //增加
    public void add(CustomerCategory cc);
    //无参查找
    public List<CustomerCategory> find();
    //修改
    public void up(CustomerCategory cc);
    //删除
    public void del(int cid);
    //有参查找
    public CustomerCategory find(CustomerCategory cc);
}

操作数据库的xml文件

<?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">
        <!--namespace填写实现的Dao接口的路径-->
<mapper namespace="com.Dao.CustomerCategoryDao">
    <!--增加-->
    <insert id="add" parameterType="CustomerCategory">
        insert into CustomerCategory values(null,#{cname},#{cdescribe},#{updateTime})
    </insert>
    <!--查找,parameterType为传入参数类型,resultType为查出的返回值类型-->
    <select id="find" parameterType="CustomerCategory" resultType="CustomerCategory">
        select * from CustomerCategory
        <where>
            <if test="cid != null">and cid = #{cid}</if>
            <if test="cname != null">and cname = #{cname}</if>
        </where>
    </select>
    <!--修改-->
    <update id="up" parameterType="CustomerCategory">
        update CustomerCategory
        <set>
            <if test="cname != null">cname = #{cname},</if>
            <if test="cdescribe != null">cdescribe = #{cdescribe},</if>
            <if test="updateTime != null">,updateTime = #{updateTime},</if>
        </set>
        where cid = #{cid}
    </update>
    <!--删除-->
    <delete id="del" parameterType="int">
        delete from CustomerCategory where cid = #{cid}
    </delete>
</mapper>

service层

service接口:

import com.Service.CustomerCategoryService;
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.IOException;
import java.io.InputStream;
import java.util.List;
public interface CustomerCategoryService {
    public void add(CustomerCategory cc) throws IOException;
    public List<CustomerCategory> select() throws IOException;
    public void up(CustomerCategory cc) throws IOException;
    public void delete(int cid) throws IOException;
    public CustomerCategory select(CustomerCategory cc) throws IOException;
}

service实现
注意:每实现一个增删改查的功能都要进行事务日志的提交

public class CustomerCategoryServiceImpl implements CustomerCategoryService {
    SqlSession sqlSession = null;
    //返回值类型为Dao层接口类
    public CustomerCategoryDao prepare() throws IOException {
        InputStream resourceAsStream = Resources.getResourceAsStream("mybatis-cfg.xml");
        SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(resourceAsStream);
        sqlSession = factory.openSession();
        //参数为Dao层接口类
        CustomerCategoryDao mapper = sqlSession.getMapper(CustomerCategoryDao.class);
        return mapper;
    }
    //事务提交和缓存清理
    public void after(){
        sqlSession.commit();
        sqlSession.clearCache();
    }
    
    @Override
    public void add(CustomerCategory cc) throws IOException {
        CustomerCategoryDao mapper = prepare();
        mapper.add(cc);
        after();
    }

    @Override
    public List<CustomerCategory> select() throws IOException {
        CustomerCategoryDao mapper = prepare();
        List<CustomerCategory> ccs = mapper.find();
        after();
        return ccs;
    }

    @Override
    public void up(CustomerCategory cc) throws IOException {
        CustomerCategoryDao mapper = prepare();
        mapper.up(cc);
        after();
    }

    @Override
    public void delete(int cid) throws IOException {
        CustomerCategoryDao mapper = prepare();
        mapper.del(cid);
        after();
    }

    @Override
    public CustomerCategory select(CustomerCategory c) throws IOException {
        CustomerCategoryDao mapper = prepare();
        CustomerCategory cc = mapper.find(c);
        after();
        return cc;
    }
}

多表查询

实体类

实体类的字段名一般与数据库的字段名一致!如果该字段为外键则使用连接表的对象

package com.Bean;

import java.sql.Timestamp;

public class User {
    private Integer uid;
    private String username;
    private String password;
    private Department department;//实体类对象
    private role r;//实体类对象
    private String isMale;
    private String mobile;
    private String address;
    private int age ;
    private String tel;
    private String idNum ;
    private String email;
    private String qq ;
    private String hobby;
    private int education ;
    private String cardNum ;
    private String nation;
    private int marry;
    private String remark;
    private Timestamp uupdateTime;
	List<Book> books;
	
    public User() {
    }
    public User(String username,String password,Department department,role r1,String isMale,String mobile,String address,int age,String tel,String idNum,String email,String qq,String hobby,int education,String cardNum,String nation,int marry,String remark,Timestamp updateTime){
        this.username = username;
        this.password = password;
        this.department = department;
        this.r = r1;
        this.isMale = isMale;
        this.mobile = mobile;
        this.address = address;
        this.age = age;
        this.tel = tel;
        this.idNum = idNum;
        this.email = email;
        this.qq = qq;
        this.hobby = hobby;
        this.education = education;
        this.cardNum = cardNum;
        this.nation = nation;
        this.marry = marry;
        this.remark = remark;
        this.uupdateTime = updateTime;
    }

    public User(Integer uid, String username, String password, Department department, role r, String isMale, String mobile, String address, int age, String tel, String idNum, String email, String qq, String hobby, int education, String cardNum, String nation, int marry, String remark, Timestamp updateTime) {
        this.uid = uid;
        this.username = username;
        this.password = password;
        this.department = department;
        this.r = r;
        this.isMale = isMale;
        this.mobile = mobile;
        this.address = address;
        this.age = age;
        this.tel = tel;
        this.idNum = idNum;
        this.email = email;
        this.qq = qq;
        this.hobby = hobby;
        this.education = education;
        this.cardNum = cardNum;
        this.nation = nation;
        this.marry = marry;
        this.remark = remark;
        this.uupdateTime = updateTime;
    }

    public Department getDepartment() {
        return department;
    }

    public void setDepartment(Department department) {
        this.department = department;
    }

    public role getR() {
        return r;
    }

    public void setR(role r) {
        this.r = r;
    }

    public Timestamp getUpdateTime() {
        return uupdateTime;
    }

    public void setUpdateTime(Timestamp updateTime) {
        this.uupdateTime = updateTime;
    }

    public String getIsMale() {
        return isMale;
    }

    public void setIsMale(String isMale) {
        this.isMale = isMale;
    }

    public String getMobile() {
        return mobile;
    }

    public void setMobile(String mobile) {
        this.mobile = mobile;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public String getTel() {
        return tel;
    }

    public void setTel(String tel) {
        this.tel = tel;
    }

    public String getIdNum() {
        return idNum;
    }

    public void setIdNum(String idNum) {
        this.idNum = idNum;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getQq() {
        return qq;
    }

    public void setQq(String qq) {
        this.qq = qq;
    }

    public String getHobby() {
        return hobby;
    }

    public void setHobby(String hobby) {
        this.hobby = hobby;
    }

    public int getEducation() {
        return education;
    }

    public void setEducation(int education) {
        this.education = education;
    }

    public String getCardNum() {
        return cardNum;
    }

    public void setCardNum(String cardNum) {
        this.cardNum = cardNum;
    }

    public String getNation() {
        return nation;
    }

    public void setNation(String nation) {
        this.nation = nation;
    }

    public int getMarry() {
        return marry;
    }

    public void setMarry(int marry) {
        this.marry = marry;
    }

    public String getRemark() {
        return remark;
    }

    public void setRemark(String remark) {
        this.remark = remark;
    }

    public User(Integer uid, String username, String password) {
        this.uid = uid;
        this.username = username;
        this.password = password;
    }

    public Integer getUid() {
        return uid;
    }

    public void setUid(Integer uid) {
        this.uid = uid;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    @Override
    public String toString() {
        return "User{" +
                "uid=" + uid +
                ", username='" + username + '\'' +
                ", password='" + password + '\'' +
                ", department=" + department +
                ", r=" + r +
                ", isMale='" + isMale + '\'' +
                ", mobile='" + mobile + '\'' +
                ", address='" + address + '\'' +
                ", age=" + age +
                ", tel='" + tel + '\'' +
                ", idNum='" + idNum + '\'' +
                ", email='" + email + '\'' +
                ", qq='" + qq + '\'' +
                ", hobby='" + hobby + '\'' +
                ", education=" + education +
                ", cardNum='" + cardNum + '\'' +
                ", nation='" + nation + '\'' +
                ", marry=" + marry +
                ", remark='" + remark + '\'' +
                ", updateTime=" + uupdateTime +
                '}';
    }
}

Dao层

Dao接口:

public interface UserDao {
    //全部查询
    public List<User> find();
    //根据id查询或根据用户名查密码
    public User find(User user);
    public void add(User user);
    public void update(User user);
    public void delete(int id);
    public List<User> search(User user);
}

操作数据库的xml文件

<?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.Dao.UserDao">
    <insert id="add" parameterType="user">
        insert into user values (null,#{username},#{password},#{department.departmentId},#{r.roleId},#{isMale},#{address},#{mobile},#{age},#{tel},#{idNum},#{email},#{qq},#{hobby},#{education},#{cardNum},#{nation},#{marry},#{remark},#{updateTime})
    </insert>
    <!--多表查询首先需要设置返回值类型,并且其中的标签有一定的顺序:id->result->association->collection->discriminator-->
    <resultMap id="udr" type="user">
        <id property="uid" column="uid"></id>
        <result property="username" column="username"></result>
        <result property="password" column="password"></result>
        <result property="isMale" column="isMale"></result>
        <result property="address" column="address"></result>
        <result property="mobile" column="mobile"></result>
        <result property="age" column="age"></result>
        <result property="tel" column="tel"></result>
        <result property="idNum" column="idNum"></result>
        <result property="email" column="email"></result>
        <result property="qq" column="qq"></result>
        <result property="hobby" column="hobby"></result>
        <result property="education" column="education"></result>
        <result property="cardNum" column="cardNum"></result>
        <result property="nation" column="nation"></result>
        <result property="marry" column="marry"></result>
        <result property="remark" column="remark"></result>
        <result property="uupdateTime" column="uupdateTime"></result>
        <!--多对一使用association-->
        <association property="department" javaType="Department">
            <id property="departmentId" column="departmentId"></id>
            <result property="departmentName" column="departmentName"></result>
            <result property="departmentDesc" column="departmentDesc"></result>
            <result property="deupdateTime" column="deupdateTime"></result>
        </association>
        <association property="r" javaType="role">
            <id property="roleId" column="roleId"></id>
            <result property="roleName" column="roleName"></result>
            <result property="roleDesc" column="roleDesc"></result>
            <result property="roleUpdateTime" column="roleUpdateTime"></result>
        </association>
        <!--对应一对多的关系使用collection-->
        <!--property跟User里属性所对应-->
        <collection property="books" javaType="list" ofType="com.zhiyou100.pojo.Book">
            <id property="book_id" column="book_id"></id>
            <result property="book_name" column="book_name"></result>
            <result property="u_id" column="u_id"></result>
        </collection>
    </resultMap>
    <select id="find" parameterType="user" resultMap="udr">
        select * from user u,department d,role r
        <where>
            <if test="true">and u.departmentId = d.departmentId</if>
            <if test="true">and u.roleId = r.roleId</if>
            <if test="uid != null">and uid = #{uid}</if>
            <if test="username != null">and username = #{username}</if>
        </where>
    </select>
    <update id="update" parameterType="user">
        update user
        <set>
            <if test="username != null">username = #{username},</if>
            <if test="password != null">password = #{password},</if>
            <if test="department.departmentId != null">departmentId = #{department.departmentId},</if>
            <if test="r.roleId != null">roleId = #{r.roleId},</if>
            <if test="isMale != null">isMale = #{isMale},</if>
            <if test="address != null">address = #{address},</if>
            <if test="mobile != null">mobile = #{mobile},</if>
            <if test="age != null">age = #{age},</if>
            <if test="tel != null">tel = #{tel},</if>
            <if test="idNum != null">idNum = #{idNum},</if>
            <if test="email != null">email = #{email},</if>
            <if test="qq != null">qq = #{qq},</if>
            <if test="hobby != null">hobby = #{hobby},</if>
            <if test="education != null">education = #{education},</if>
            <if test="cardNum != null">cardNum = #{cardNum},</if>
            <if test="nation != null">nation = #{nation},</if>
            <if test="marry != null">marry = #{marry},</if>
            <if test="remark != null">remark = #{remark},</if>
            <if test="uupdateTime != null">uupdateTime = #{uupdateTime},</if>
        </set>
        where uid = #{uid}
    </update>
    <delete id="delete" parameterType="int">
        delete from user where uid = #{uid}
    </delete>
    <select id="search" parameterType="user" resultMap="udr">
        select * from user,department,role
        <where>
            <if test="true">and user.departmentId = department.departmentId</if>
            <if test="true">and user.roleId = role.roleId and</if>
            <!--choose标签表示下面的内容只能选择一个,if只要test条件满足可以选择多个-->
          <choose>
              <when test="username != null">username like "%"#{username}"%"</when>
              <when test="mobile != null">mobile like "%"#{mobile}"%"</when>
              <when test="address != null">address like "%"#{address}"%"</when>
          </choose>
        </where>
    </select>
</mapper>

service层与上面差不多,就不展示了

猜你喜欢

转载自blog.csdn.net/qq_34191426/article/details/88355946