栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

详细笔记的第一遍:学习ssm的整合-CRUD的第2天(2021-11-22)1

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

详细笔记的第一遍:学习ssm的整合-CRUD的第2天(2021-11-22)1

详细笔记的第一遍:学习ssm的整合-CRUD的第2天(2021-11-22)1 现在自动生成的一些mapper.xml文件有些需要进行修改。

1、目前查询员工表的信息的时候,他只会有员工的一些字段,而没有关联的部门信息。
所以我们需要自己建一个方法,来进行联合查询。
在EmployeeMapper.java文件里面再添加两个方法:

public interface EmployeeMapper {
List selectByExampleWithDept(EmployeeExample example);

    Employee selectByPrimaryKeyWithDept(Integer empId);
    }

2、修改Employee.java
添加属性Department
再添加get/set方法。

3、 在EmployeeMapper.xml里面写两个方法对应的查询sql
selectByExampleWithDept

selectByPrimaryKeyWithDept

所以,目前我们提供了
查询员工信息的同时不带部门信息和
查询员工信息的同时带上部门信息

EmployeeMapper.xml文件中多加的内容:
1、自定义了resultMap ,除了Employee bean里面的基本类型的属性,还使用association 添加了引用类型属性Department

 
    
    
    
    
    
    
      
      
    
  

2、sql别名。之前只有基本Employee,现在多加了两个部门里面的属性。

 
    e.emp_id, e.emp_name, e.gender, e.email, e.d_id, d.id, d.dept_name
  

3、多了两个最重要的select标签。
selectByExampleWithDept和selectByPrimaryKeyWithDept


    select
    
    FROM tbl_emp e
    LEFT JOIN tbl_dept d
    ON e.emp_id = d.id
    where emp_id = #{empId,jdbcType=INTEGER}
  
11、搭建Spring的单元测试环境

我们推荐,在Spring项目里面,就可以使用Spring的单元测试,这样可以自动注入我们需要的组件。
1、在pom文件里面导入spring单元测试的依赖

 
            org.springframework
            spring-test
            4.3.7.RELEASE
        

目前项目的pom文件



    4.0.0

    com.rtl
    ssm-crud
    1.0-SNAPSHOT
    war

    
        8
        8
    
    
        
            org.springframework
            spring-webmvc
            4.3.7.RELEASE
        
        
            org.springframework
            spring-jdbc
            4.3.7.RELEASE
        
        
            org.springframework
            spring-aspects
            4.3.7.RELEASE
        

        
            org.mybatis
            mybatis
            3.4.2
        

        
            org.mybatis
            mybatis-spring
            1.3.1
        

        
            c3p0
            c3p0
            0.9.1.2
        

        
            mysql
            mysql-connector-java
            8.0.27
        
        
            jstl
            jstl
            1.2
        

        
            javax.servlet
            servlet-api
            2.5
            provided
        

        
            junit
            junit
            4.12
            test
        
        
            org.mybatis.generator
            mybatis-generator-core
            1.3.5
        

        
            org.springframework
            spring-test
            4.3.7.RELEASE
        


    


2、new 一个类 MapperTest
并且在这个类上面写上注解:@ContextConfiguration

写上spring的配置文件的存放路径
@ContextConfiguration(locations = {"classpath:applicationContext.xml"})


注意:之前引入的juint的范围都写得是test
现在改一下:

 
            junit
            junit
            4.12
        

再使用另外一个注解@RunWith

@RunWith(SpringJUnit4ClassRunner.class)

接下来直接写@Autowired就可以自动注入了
而不需要再写:

 ClassPathXmlApplicationContext ioc = new ClassPathXmlApplicationContext("applicationContext.xml");
        DepartmentMapper departmentMapper = ioc.getBean(DepartmentMapper.class);

这样的代码才能获取对象。

直接使用注解@Autowired即可。

好了,基本的搭建Spring的测试框架:
整个测试类MapperTest:

package com.rtl.crud.test;

import com.rtl.crud.dao.DepartmentMapper;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"classpath:applicationContext.xml"} )
public class MapperTest {

    @Autowired
    DepartmentMapper departmentMapper;

    //测试DepartmentMapper
    @Test
    public void testCRUD(){
        System.out.println(departmentMapper);
    }

}

这里经历了一个困难:
就是生成的mapper.xml文件,会出现重复id的情况。
检测原因是mybatis的使用版本过低。
但是把mybatis的版本提高,又报另一个错误。

Error creating bean with name 'sqlSessionFactory' defined in class path resource [applicationContext.xml]: Invocation of init method failed; nested exception is java.lang.NoSuchMethodError: org.apache.ibatis.session.Configuration.setDefaultEnumTypeHandler(Ljava/lang/Class;)V

最终的pom里面的mybatis版本:
mybatis-spring 1.3.1
mybatis-generator-core 1.3.7

 
            org.mybatis
            mybatis-spring
            1.3.1
        
        
            org.mybatis.generator
            mybatis-generator-core
            1.3.7
        

测试类:

可以通过搭建spring的单元测试环境,而不需要使用new ClassPathXmlApplicationContext来获取对象。

测试成功!!!
最终的pom文件:



    4.0.0

    com.rtl
    ssm-crud
    1.0-SNAPSHOT
    war

    
        8
        8
    
    
        
            org.springframework
            spring-webmvc
            4.3.7.RELEASE
        
        
            org.springframework
            spring-jdbc
            4.3.7.RELEASE
        
        
            org.springframework
            spring-aspects
            4.3.7.RELEASE
        

        
            org.mybatis
            mybatis
            3.4.2
        

        
            org.mybatis
            mybatis-spring
            1.3.1
        
        
            org.mybatis.generator
            mybatis-generator-core
            1.3.7
        

        
            c3p0
            c3p0
            0.9.1.2
        

        
            mysql
            mysql-connector-java
            8.0.27
        
        
            jstl
            jstl
            1.2
        

        
            javax.servlet
            servlet-api
            2.5
            provided
        

        
            junit
            junit
            4.12
        


        
            org.springframework
            spring-test
            4.3.7.RELEASE
        


    


开始测试:插入数据到部门表
注意:
因为我们部门表里面的id字段是自增属性

所以我们在插入数据的时候,id这个字段写为Null

1、先给Department类里面加上无参和有参构造器。

2、再给测试类里面写插入数据的代码。


所以,往表tbl_dept里面插入数据成功。

测试插入员工表的数据
1、加上构造器

2、测试类:



测试插入数据到员工表成功!!!

批量插入多个员工信息。
11

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/582856.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号