XML版本:

实体类:

package com.sunwii.mybatis.bean;

import java.util.ArrayList;
import java.util.List;

import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.ToString;

@Data
@ToString
@NoArgsConstructor
public class Dept {
    private Integer id;
    private String name;
    private List<Employee> employees = new ArrayList<Employee>();
    
    public Dept(Integer id) {
        this.id = id;
    }
    
    public String toLazyString() {
        return "Dept:{id: "+this.id+" ; name: "+this.name+"}";
    }
}

 

 

package com.sunwii.mybatis.bean;

import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@NoArgsConstructor
public class Employee {
    protected Integer id;
    protected String name;
    protected Dept dept;

    public Employee(Integer id) {
        this.id = id;
    }

    public String toLazyString() {
        return "Employee:{id: " + this.id + "; name: " + this.name + "}";
    }

    @Override
    public String toString() {
        return "Employee(id=" + this.id + ", name=" + this.name + ", dept={id=" + this.dept.getId() + ", name="
                + this.dept.getName() + "})";
    }
}

Mapper接口:

package com.sunwii.mybatis.mapper;

import com.sunwii.mybatis.bean.Dept;

public interface DeptMapper {
    public Dept selectById(Integer id);
    public int insertDept(Dept dept);
    public int updateDept(Dept dept);
    public int deleteDept(Dept dept);
}

 

package com.sunwii.mybatis.mapper;

import com.sunwii.mybatis.bean.Employee;

public interface EmployeeMapper {
    public Employee selectById(Integer id);
    public int insertEmployee(Employee employee);
    public int updateEmployee(Employee employee);
    public int deleteEmployee(Employee employee);
}

 

Mapper映射文件:

<?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.sunwii.mybatis.mapper.DeptMapper">
    <resultMap type="Dept" id="DeptMap_basic">
        <id property="id" column="did" />
        <result property="name" column="name" />
    </resultMap>
    <resultMap type="Dept" id="DeptMap" extends="DeptMap_basic">
         <!-- 一对多关联:使用select引用方式 -->
        <collection property="employees" column="did" ofType="Employee" select="com.sunwii.mybatis.mapper.EmployeeMapper.selectByDept" fetchType="lazy">
        </collection>
    </resultMap>
    
    <select id="selectById" parameterType="Integer"
        resultMap="DeptMap">
        select id as did, name from t_dept d where d.id=#{id}
    </select>

    <insert id="insertDept" parameterType="Dept" keyColumn="id"
        keyProperty="id" useGeneratedKeys="true">
        insert into t_dept(name)
        values(#{name})
    </insert>


    <update id="updateDept" parameterType="Dept">
        update t_dept set
        name=#{name}
        where id=#{id}
    </update>

    <delete id="deleteDept" parameterType="Dept">
        delete from t_dept
        where
        id=#{id}
    </delete>
</mapper>

 

 

<?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.sunwii.mybatis.mapper.EmployeeMapper">
    <resultMap type="Employee" id="EmployeeMap_basic">
        <id property="id" column="id" />
        <result property="name" column="name" />
    </resultMap>
    <resultMap type="Employee" id="EmployeeMap" extends="EmployeeMap_basic">
        <!-- 多对一关联:使用select引用方式 -->
        <association property="dept" column="dept_id"
            javaType="Dept"
            select="com.sunwii.mybatis.mapper.DeptMapper.selectById"
            fetchType="lazy" />
    </resultMap>
    <select id="selectById" parameterType="Integer"
        resultMap="EmployeeMap">
        select id, name, dept_id from t_employee e where e.id=#{id}
    </select>
    <select id="selectById2" parameterType="Integer"
        resultMap="EmployeeMap">
        select e.id id, e.name name,d.id did,d.name dname from
        t_employee e
        inner join t_dept
        d on e.dept_id=d.id and e.id=#{id}
    </select>

    <select id="selectByDept" parameterType="Integer"
        resultMap="EmployeeMap_basic">
        select id, name, dept_id from t_employee e where
        e.dept_id=#{dept.id}
    </select>

    <insert id="insertEmployee" parameterType="Employee"
        keyColumn="id" keyProperty="id" useGeneratedKeys="true">
        insert into t_employee(name
        <if test="dept!=null">
            ,dept_id
        </if>
        )
        values(#{name}
        <if test="dept!=null">
            ,#{dept.id}
        </if>
        )
    </insert>


    <update id="updateEmployee" parameterType="Employee">
        update t_employee set
        name=#{name} ,dept_id=#{dept.id}
        where id=#{id}
    </update>

    <delete id="deleteEmployee" parameterType="Employee">
        delete from t_employee
        where id=#{id}
    </delete>
</mapper>

 

业务层接口实现类:

package com.sunwii.mybatis.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.sunwii.mybatis.bean.Dept;
import com.sunwii.mybatis.mapper.DeptMapper;
import com.sunwii.mybatis.service.DeptService;

@Service
public class DeptServiceImpl implements DeptService {
    @Autowired
    private DeptMapper deptMapper;

    @Override
    public Dept getDept(Integer id) {
        return deptMapper.selectById(id);
    }

    @Override
    @Transactional
    public void insertDept(Dept dept) {
        deptMapper.insertDept(dept);
    }

    @Override
    @Transactional
    public void updateDept(Dept dept) {
        deptMapper.updateDept(dept);
    }

    @Override
    @Transactional
    public void deleteDept(Dept dept) {
        deptMapper.deleteDept(dept);
    }

}

 

package com.sunwii.mybatis.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.sunwii.mybatis.mapper.DeptMapper;
import com.sunwii.mybatis.mapper.EmployeeMapper;
import com.sunwii.mybatis.service.EmployeeService;
import com.sunwii.mybatis.bean.*;

@Service
public class EmployeeServiceImpl implements EmployeeService {
    @Autowired
    private EmployeeMapper employeeMapper;
    @Autowired
    private DeptMapper deptMapper;

    @Override
    public Employee getEmployee(Integer id) {
        return employeeMapper.selectById(id);
    }

    @Override
    @Transactional
    public void insertEmployee(Employee employee) {
        Dept dept = employee.getDept();
        //如果需要的话,就先新增部门
        if(dept!=null && dept.getId()==null) {
            deptMapper.insertDept(dept);
        }
        
        employeeMapper.insertEmployee(employee);
    }

    @Override
    @Transactional
    public void updateEmployee(Employee employee) {
        employeeMapper.updateEmployee(employee);
    }

    @Override
    @Transactional
    public void deleteEmployee(Employee employee) {
        employeeMapper.deleteEmployee(employee);
    }

}

 

Spring配置文件:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:p="http://www.springframework.org/schema/p"
    xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:mybatis="http://mybatis.org/schema/mybatis-spring"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
    http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
    http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd
    http://mybatis.org/schema/mybatis-spring http://mybatis.org/schema/mybatis-spring.xsd">

    <!-- 引入jdbcs配置文件 -->
    <context:property-placeholder
        location="classpath:jdbc.properties" />

    <!-- 数据库连接池 -->
    <bean id="dataSource"
        class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
        <property name="driverClassName" value="${driver}" />
        <property name="url" value="${url}" />
        <property name="username" value="${user}" />
        <property name="password" value="${password}" />
        <property name="maxActive" value="210" />
        <property name="maxIdle" value="50" />
    </bean>

    <!-- mybatis -->
    <bean id="sessionFactory"
        class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource" />
        <property name="configLocation"
            value="classpath:mybatis-config.xml" />
        <property name="mapperLocations"
            value="classpath:com/sunwii/mybatis/bean/*.xml" />
    </bean>

    <!-- Mapper动态代理开发扫描 -->
    <mybatis:scan base-package="com.sunwii.mybatis.mapper" />

    <!-- 事务管理器 -->
    <bean id="transactionManager"
        class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource" />
    </bean>

    <!-- 事务注解 -->
    <tx:annotation-driven
        transaction-manager="transactionManager" />

    <!-- 组件扫描 -->
    <!-- Service扫描 -->
    <context:component-scan
        base-package="com.sunwii.mybatis.service.impl" />
</beans>

 

Mybatis配置文件:

<?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>
    <settings>
        <setting name="lazyLoadingEnabled" value="true" />
        <setting name="aggressiveLazyLoading" value="false" />
    </settings>
    
    <typeAliases>
        <package name="com.sunwii.mybatis.bean" />
    </typeAliases>
    
</configuration>

 

测试类:

package com.sunwii.mybatis.test.one2many;

import org.junit.Test;
import org.springframework.context.ApplicationContext;

import com.sunwii.mybatis.bean.Dept;
import com.sunwii.mybatis.bean.Employee;
import com.sunwii.mybatis.service.DeptService;
import com.sunwii.mybatis.service.EmployeeService;
import com.sunwii.mybatis.util.SpringUtil;

public class TestOne2Many {
    private ApplicationContext context = SpringUtil.getContext();
    private EmployeeService employeeService = (EmployeeService)context.getBean(EmployeeService.class);
    private DeptService deptService = (DeptService)context.getBean(DeptService.class);
    
    @Test
    public void testEmployeeInsert() {
        // 多对一:添加。
        Employee employee = new Employee();
        employee.setName("e-1");
        Dept dept = deptService.getDept(1);
        employee.setDept(dept);
        
        employeeService.insertEmployee(employee);
        
    }
    

    @Test
    public void testEmployeeInsert2() {
        // 多对一:添加。
        Employee employee = new Employee();
        employee.setName("e-2");
        //Dept dept = deptService.getDept(1);
        //employee.setDept(dept);
        
        employeeService.insertEmployee(employee);
        
        
    }

    @Test
    public void testEmployeeSelect() {
        // 多对一:查询。
        int id = 16;
        Employee employee = employeeService.getEmployee(id);
        
        System.out.println(employee.toLazyString());
        System.out.println(employee);
    }
    


    @Test
    public void testEmployeeUpdate() {
        // 多对一:更新。
        int id = 15;
        Employee employee = employeeService.getEmployee(id);
        employee.setName("eeeeeeeeeeeee15");
        employeeService.updateEmployee(employee);
    }
    
    

    @Test
    public void testEmployeeDelete() {
        // 多对一:删除。
        int id=15;
        Employee employee = new Employee(id);
        employeeService.deleteEmployee(employee);
        
    }
    

    @Test
    public void testDeptSelect() {
        // 一对多:更新。
        int id=1;
        Dept dept = deptService.getDept(id);
        System.out.println(dept.toLazyString());
        System.out.println(dept);
    }

    @Test
    public void testDeptUpdate() {
        // 一对多:更新。
        int id=2;
        Dept dept = new Dept(id);
        dept.setName("ddddddddd2");
        deptService.updateDept(dept);
    }
}

 

注解版本:

注解版本只是将Mapper映射文件去掉,将映射注解到Mapper接口中(并使用了动态sql提供器),其它东西不变。

Mapper接口(注解版):

package com.sunwii.mybatis.mapper;

import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Many;
import org.apache.ibatis.annotations.Options;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.apache.ibatis.mapping.FetchType;

import com.sunwii.mybatis.bean.Dept;

public interface DeptMapper {
    public static final String select = "select id,name from t_dept where id=#{id}";
    public static final String insert = "insert into t_dept(name) values(#{name})";
    public static final String update = "update t_dept set name=#{name}";
    public static final String delete = "delete from t_dept where id=#{id}";
    
    public static final String one2many = "com.sunwii.mybatis.mapper.EmployeeMapper.selectByDept";
    
    @Select(select)
    @Results(id = "DeptMap", value= {
            @Result(property = "id", column = "id"),
            @Result(property = "name", column = "name"),
            @Result(property = "employees", column = "id",
            many = @Many(
                    select = one2many,
                    fetchType = FetchType.LAZY))
    })
    public Dept selectById(Integer id);

    @Insert(insert)
    @Options(keyColumn = "id", keyProperty = "id", useGeneratedKeys = true)
    public int insertDept(Dept dept);

    @Update(update)
    public int updateDept(Dept dept);

    @Delete(delete)
    public int deleteDept(Dept dept);
}

 

 

package com.sunwii.mybatis.mapper;

import java.util.List;

import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.InsertProvider;
import org.apache.ibatis.annotations.One;
import org.apache.ibatis.annotations.Options;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.apache.ibatis.mapping.FetchType;

import com.sunwii.mybatis.bean.Employee;
import com.sunwii.mybatis.provider.EmployeeDynamicSqlProvider;

public interface EmployeeMapper {
    public static final String select = "select id,name,dept_id from t_employee where id=#{id}";
    public static final String selectByDept = "select id,name from t_employee where dept_id=#{deptId}";
    public static final String insert = null;// dynamic sql provider
    public static final String update = "update t_employee set name=#{name},dept_id=#{dept.id}";
    public static final String delete = "delete from t_employee where id=#{id}";
    
    public static final String many2one = "com.sunwii.mybatis.mapper.DeptMapper.selectById";
    
    @Select(select)
    @Results(id="EmployeeMap", value= {
            @Result(property = "id",column = "id"),
            @Result(property = "name", column = "name"),
            @Result(property = "dept",column = "dept_id",
                one = @One(
                        select = many2one,
                        fetchType = FetchType.LAZY))
    })
    public Employee selectById(Integer id);
    
    @Select(selectByDept)
    @Results(id="EmployeeMap_basic", value= {
            @Result(property = "id",column = "id"),
            @Result(property = "name", column = "name")
    })
    public List<Employee> selectByDept(Integer deptId);

    @InsertProvider(type = EmployeeDynamicSqlProvider.class, method = "insert")
    @Options(keyColumn = "id", keyProperty = "id", useGeneratedKeys = true)
    public int insertEmployee(Employee employee);

    @Update(update)
    public int updateEmployee(Employee employee);

    @Delete(delete)
    public int deleteEmployee(Employee employee);
}

 

动态SQL提供器:

package com.sunwii.mybatis.provider;

import org.apache.ibatis.jdbc.SQL;

import com.sunwii.mybatis.bean.Employee;

public class EmployeeDynamicSqlProvider {
    private SQL NOTNULLSET(String columnAndValue, SQL sqlObject, Object object) {
        if(object!=null) {
            return sqlObject.SET(columnAndValue);
        }
        
        return sqlObject;
    }
    
    private SQL NOTNULLVALUES(String column, String value, SQL sqlObject, Object object) {
        if(object!=null) {
            return sqlObject.VALUES(column,value);
        }
        
        return sqlObject;
    }
    
    
    public String insert(Employee employee) {
        return new SQL() {
            {
                INSERT_INTO("t_employee");
                VALUES("name", "#{name}");
                
                NOTNULLVALUES("dept_id","#{dept.id}",this, employee.getDept());
            }
        }.toString();
    }
}

 

 

多对多的示例:

https://www.cnblogs.com/dreamyoung/p/11804936.html

posted on 2019-11-06 10:32  Dream Young  阅读(3231)  评论(0编辑  收藏  举报