MyBatis学习笔记

一   配置文件取别名的两种方式

  

<!-- <typeAliases>
<typeAlias alias="Student" type="com.he.entity.Student"/>
</typeAliases>- -->
<typeAliases>
<package name="com.he.entity"/>
</typeAliases>

 

二   数据源配置两种方式

<transactionManager type="JDBC" />
<dataSource type="POOLED">
<property name="driver" value="${jdbc.driverClassName}" />
<property name="url" value="${jdbc.url}" />
<property name="username" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
</dataSource>

 

 

<transactionManager type="JDBC" />
<dataSource type="POOLED">
<property name="driver" value="${jdbc.driverClassName}" />
<property name="url" value="${jdbc.url}" />
<property name="username" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
</dataSource>

 

三   可以配置多种环境   例如:开发环境,测试环境,生产环境

<environments default="development">
<environment id="development">
<transactionManager type="JDBC" />
<dataSource type="POOLED">
<property name="driver" value="${jdbc.driverClassName}" />
<property name="url" value="${jdbc.url}" />
<property name="username" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
</dataSource>
</environment>
<environment id="test">
<transactionManager type="JDBC" />
<dataSource type="POOLED">
<property name="driver" value="${jdbc.driverClassName}" />
<property name="url" value="${jdbc.url}" />
<property name="username" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
</dataSource>
</environment>
</environments>

 

四   映射配置----两种方式

<mappers>
<mapper resource="com/he/mappers/StudentMapper.xml" />
</mappers>

 

<mappers>
<package name="com.he.mappers"/>
</mappers>

 五  mapper里面的方法怎么写

XML:

<insert id="add" parameterType="Student" >
insert into student values(#{id},#{name},#{age})
</insert>

<update id="update" parameterType="Student">
update student set name=#{name},age=#{age} where id=#{id}
</update>

<delete id="delete" parameterType="Integer">
delete from student where id=#{id}
</delete>

<select id="findAll" resultType="Student">
select * from student
</select>

mapper.java


public interface StudentMapper {

public int add(Student student);

public int update(Student student);

public int delete(Integer id);

public List<Student> findAll();

}

 

六  一对一关系

Entity : student.java    Address.java

表: student    address

package com.he.entity;

public class Student {
    private int id;
    private String name;
    private int age;
    
    private  Address address;
    
    public Student(){}
    
    public Student(int id, String name, int age) {
        super();
        this.id = id;
        this.name = name;
        this.age = age;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

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

    
    public Address getAddress() {
        return address;
    }

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

    @Override
    public String toString() {
        return "Student [id=" + id + ", name=" + name + ", age=" + age
                + ", address=" + address + "]";
    }


    
    

}
View Code
package com.he.entity;

public class Address {
    private String id;
    private String sheng;
    private String shi;
    private String qu;
    
    public Address(){}
    
    public Address(String id, String sheng, String shi, String qu) {
        super();
        this.id = id;
        this.sheng = sheng;
        this.shi = shi;
        this.qu = qu;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getSheng() {
        return sheng;
    }

    public void setSheng(String sheng) {
        this.sheng = sheng;
    }

    public String getShi() {
        return shi;
    }

    public void setShi(String shi) {
        this.shi = shi;
    }

    public String getQu() {
        return qu;
    }

    public void setQu(String qu) {
        this.qu = qu;
    }

    @Override
    public String toString() {
        return "Address [id=" + id + ", sheng=" + sheng + ", shi=" + shi
                + ", qu=" + qu + "]";
    }
    

}
View Code

 

 

mapper.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.he.mappers.StudentMapper">

    <resultMap type="Student" id="StudentRes">
        <id property="id" column="id"/>
        <result property="name"  column="name"/>
        <result property="age"  column="age"/>
        
        <result property="address.id" column="address"/>
        <result property="address.sheng" column="sheng"/>
        <result property="address.shi" column="shi"/>
        <result property="address.qu" column="qu"/>
    </resultMap>
    
    <select id="findStudentWithAddress" parameterType="Integer" resultMap="StudentRes">
        select * from student s,address a where s.address=a.id and s.id=#{id}
    </select>

    <insert id="add" parameterType="Student"  >
        insert into student values(#{id},#{name},#{age})
    </insert>
    
    <update id="update" parameterType="Student">
        update student set name=#{name},age=#{age} where id=#{id}
    </update>
    
    <delete id="delete" parameterType="Integer">
        delete from student where id=#{id}
    </delete>
    
    <select id="findAll" resultType="Student">
        select * from  student
    </select>

</mapper> 
View Code

 

package com.he.mappers;

import java.util.List;

import com.he.entity.Student;

public interface StudentMapper {
    
    public int add(Student student);
    
    public int update(Student student);
    
    public int delete(Integer id);
    
    public List<Student> findAll();
    
    public Student findStudentWithAddress(Integer id);

}
View Code

 最好的写法是将Address也弄一个类和xml

studentmapper.xml中这样写:

<resultMap type="Student" id="StudentRes">
<id property="id" column="id"/>
<result property="name" column="name"/>
<result property="age" column="age"/>
<association property="address" column="address" select="com.he.mappers.AddressMapper.findById"></association>
</resultMap>

<select id="findStudentWithAddress" parameterType="Integer" resultMap="StudentRes">
select * from student s,address a where s.address=a.id and s.id=#{id}
</select>

 

多对多

<resultMap type="Grade" id="GradeRes">
<result property="id" column="id"/>
<result property="gradeName" column="gradeName"/>
<collection property="studentList" column="id" select="com.he.mappers.StudentMapper.findStudentByGradeId"></collection>
</resultMap>

<select id="findById" parameterType="Integer" resultType="Grade">
select * from address where id=#{id}
</select>

 

<select id="findStudentWithAddress" parameterType="Integer" resultMap="StudentRes">
select * from student s,address a where s.address=a.id and s.id=#{id}
</select>

 

posted on 2016-11-13 18:26  编世界  阅读(107)  评论(0编辑  收藏  举报