MyEclipse整合SSM框架(二):Mybatis逆向工程配置以及dao层增删改查测试

说明:项目工程采用 maven 管理,maven 工程的建立参见:

           https://blog.csdn.net/weixin_38533896/article/details/7976818

           Spring + SpringMVC+ Mybatis 相关 .xml 文件配置参见: 

           https://blog.csdn.net/weixin_38533896/article/details/79863620

           本文是接续上两篇博客完成的。

1.  建立数据表 


2.  工程中按照功能建立对应的包,在src/main/resources下建立 mapper 文件夹用于存放逆向工程的映射文件

 

3.  引入mybatis-generater jar 包,此 jar包的引入已经写入 pom.xml 文件

  1. <dependency>  
  2.     <groupId>org.mybatis.generator</groupId>  
  3.     <artifactId>mybatis-generator-core</artifactId>  
  4.     <version>1.3.5</version>  
  5. </dependency> 

4.  在工程的根目录建立 mbg.xml 文件,配置如下: 

  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <!DOCTYPE generatorConfiguration  
  3.   PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN"  
  4.   "http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd">  
  5.   
  6. <generatorConfiguration>  
  7.   
  8.     <context id="DB2Tables" targetRuntime="MyBatis3">  
  9.   
  10.         <!-- 屏蔽生成代码的注释 -->  
  11.         <commentGenerator>  
  12.             <property name="suppressAllComments" value="true" />  
  13.         </commentGenerator>  
  14.   
  15.         <!-- 配置数据库链接 -->  
  16.         <jdbcConnection driverClass="com.mysql.jdbc.Driver"  
  17.             connectionURL="jdbc:mysql://localhost:3306/ssm?characterEncoding=UTF-8&useSSL=false"  
  18.             userId="root" password="402664107">  
  19.         </jdbcConnection>  
  20.   
  21.         <javaTypeResolver>  
  22.             <property name="forceBigDecimals" value="false" />  
  23.         </javaTypeResolver>  
  24.   
  25.         <!-- 指定javaBean 的生成位置 -->  
  26.         <javaModelGenerator targetPackage="com.lbc.crud.bean"  
  27.             targetProject=".\src\main\java">  
  28.             <property name="enableSubPackages" value="true" />  
  29.             <property name="trimStrings" value="true" />  
  30.         </javaModelGenerator>  
  31.   
  32.         <!-- 指定sql 映射文件生成位置 -->  
  33.         <sqlMapGenerator targetPackage="mapper" targetProject=".\src\main\resources">  
  34.             <property name="enableSubPackages" value="true" />  
  35.         </sqlMapGenerator>  
  36.   
  37.         <!-- 指定dao 接口生成的位置,mapper 接口 -->  
  38.         <javaClientGenerator type="XMLMAPPER"  
  39.             targetPackage="com.lbc.crud.dao" targetProject=".\src\main\java">  
  40.             <property name="enableSubPackages" value="true" />  
  41.         </javaClientGenerator>  
  42.   
  43.         <!-- table 指定每个表的生成策略 -->  
  44.         <table tableName="tbl_emp" domainObjectName="Employee"></table>  
  45.         <table tableName="tbl_dept" domainObjectName="Department"></table>  
  46.   
  47.     </context>  
  48. </generatorConfiguration>  

5.  在 test 包下建立MBGTest.java,运行生成数据表对应的 bean、dao

  1. public class MBGTest {  
  2.   
  3.     public static void main(String[] args) throws Exception {  
  4.         List<String> warnings = new ArrayList<String>();  
  5.         boolean overwrite = true;  
  6.         File configFile = new File("mbg.xml");  
  7.         ConfigurationParser cp = new ConfigurationParser(warnings);  
  8.         Configuration config = cp.parseConfiguration(configFile);  
  9.         DefaultShellCallback callback = new DefaultShellCallback(overwrite);  
  10.         MyBatisGenerator myBatisGenerator = new MyBatisGenerator(config,  
  11.                 callback, warnings);  
  12.         myBatisGenerator.generate(null);  
  13.     }  
  14. }  


6.  在 test 包下建立 MapperTest.java 测试增删改查。注意:需要导入 Spring 单元测试 jar 包,此 jar 包导入已写入pom.xml

  1. <!-- spring单元测试 -->  
  2. <dependency>  
  3.     <groupId>org.springframework</groupId>  
  4.     <artifactId>spring-test</artifactId>  
  5.     <version>4.3.7.RELEASE</version>  
  6.     <scope>test</scope>  
  7. </dependency>  

MapperTest.java

  1. @RunWith(SpringJUnit4ClassRunner.class)  
  2. @ContextConfiguration(locations = { "classpath:applicationContext.xml" })  
  3. public class MapperTest {  
  4.   
  5.     @Autowired  
  6.     DepartmentMapper departmentMapper;  
  7.   
  8.     @Autowired  
  9.     EmployeeMapper employeeMapper;  
  10.   
  11.     @Autowired  
  12.     SqlSession sqlSession;  
  13.   
  14.     @Test  
  15.     public void testDeptInsert() {  
  16.         int insertSelective = departmentMapper.insertSelective(new Department(null"开发部"));  
  17.         System.out.println(insertSelective);  
  18.     }  
  19.   
  20.     @Test  
  21.     public void testDeptSelect() {  
  22.         Department selectByPrimaryKey = departmentMapper.selectByPrimaryKey(1);  
  23.         System.out.println(selectByPrimaryKey);  
  24.     }  
  25.   
  26.     @Test  
  27.     public void testEmpSelectByKey() {  
  28.         Employee selectByPrimaryKeyWithDept = employeeMapper  
  29.                 .selectByPrimaryKeyWithDept(2);  
  30.         System.out.println(selectByPrimaryKeyWithDept);  
  31.     }  
  32.   
  33.     @Test  
  34.     public void testEmpSelectByExample() {  
  35.           
  36.         EmployeeExample employeeExample = new EmployeeExample();  
  37.         Criteria criteria = employeeExample.createCriteria();  
  38.         criteria.andEmpIdLessThan(5);  
  39.   
  40.         List<Employee> listTeammembers =employeeMapper.selectByExampleWithDept(employeeExample);    
  41.         for(Employee e : listTeammembers){            
  42.             System.out.println(e);  
  43.         }  
  44.     }  
  45.   
  46.     @Test  //测试批量插入
  47.     public void testEmpInsert() {  
  48.         EmployeeMapper mapper = sqlSession.getMapper(EmployeeMapper.class);  
  49.         for (int i = 0; i < 1000; i++) {  
  50.             String uid = UUID.randomUUID().toString().substring(05) + i;  
  51.             mapper.insertSelective(new Employee(null, uid, "M", uid  
  52.                     + "@atguigu.com"1));  
  53.         }  
  54.     }  
  55.   


7.  测试结果



至此,SSM 框架已经实现 控制台与数据库的接通,接下来进行控制台与前端界面的联合调试。


评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值