搭建环境

配置maven依赖的架包

 tk.mybatismapper4.0.0-beta3junitjunit4.9log4jlog4j1.2.17cglibcglib2.2org.aspectjaspectjweaver1.6.8org.slf4jslf4j-api1.7.7org.slf4jslf4j-log4j121.7.7org.mybatismybatis3.2.8org.mybatismybatis-spring1.2.2com.mchangec3p00.9.2org.springframeworkspring-context4.3.10.RELEASEmysqlmysql-connector-java5.1.37org.springframeworkspring-orm4.3.10.RELEASE

Spring整合mybatis

<?xml  version="1.0" encoding="UTF-8"?>configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd">

Spring的配置文件

<?xml  version="1.0" encoding="UTF-8"?>"http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd  http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd  http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd  http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd">"classpath:jdbc.properties"/>"dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">"user" value="${jdbc.user}"/>"password" value="${jdbc.password}"/>"jdbcUrl" value="${jdbc.url}"/>"driverClass" value="${jdbc.driver}"/>"sqlSessionFactoryBean" class="org.mybatis.spring.SqlSessionFactoryBean">"configLocation" value="classpath:mybatis-config.xml"/>"dataSource" ref="dataSource"/>class="tk.mybatis.spring.mapper.MapperScannerConfigurer">"basePackage" value="com.yang.mapper.mappers"/>package="com.yang.mapper.services"/>"dataSourceTransactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">"dataSource" ref="dataSource"/>"txAdvice" pointcut="execution(* *..*Service.*(..))"/>"txAdvice" transaction-manager="dataSourceTransactionManager">"get*" read-only="true"/>"save*" rollback-for="java.lang.Exception" propagation="REQUIRES_NEW"/>"remove*" rollback-for="java.lang.Exception" propagation="REQUIRES_NEW"/>"update*" rollback-for="java.lang.Exception" propagation="REQUIRES_NEW"/>

整合log4j.properties配置文件

log4j.rootLogger=DEBUG,myConsolelog4j.appender.myConsole=org.apache.log4j.ConsoleAppenderlog4j.appender.myConsole.ImmediateFlush=truelog4j.appender.myConsole.Target=System.outlog4j.appender.myConsole.layout=org.apache.log4j.PatternLayoutlog4j.appender.myConsole.layout.ConversionPattern=[%-5p] %d(%r) --> [%t] %l: %m %x %nlog4j.logger.com.mchange.v2=ERROR

数据库jdbc.properties配置文件

jdbc.user=rootjdbc.password=rootjdbc.url=jdbc:mysql://localhost:3306/girls?useUnicode=true&characterEncoding=utf8jdbc.driver=com.mysql.jdbc.Driver

数据库表的设计

CREATE TABLE `tabple_emp` (`emp_id` int NOT NULL AUTO_INCREMENT , `emp_name` varchar(500) NULL ,`emp_salary` double(15,5) NULL ,`emp_age` int NULL , PRIMARY KEY (`emp_id`) );INSERT INTO `tabple_emp` (`emp_name`, `emp_salary`, `emp_age`) VALUES ('tom', '1254.37', '27');INSERT INTO `tabple_emp` (`emp_name`, `emp_salary`, `emp_age`) VALUES ('jerry', '6635.42', '38'); INSERT INTO `tabple_emp` (`emp_name`, `emp_salary`, `emp_age`) VALUES ('bob', '5560.11', '40'); INSERT INTO `tabple_emp` (`emp_name`, `emp_salary`, `emp_age`) VALUES ('kate', '2209.11', '22');INSERT INTO `tabple_emp` (`emp_name`, `emp_salary`, `emp_age`) VALUES ('justin', '4203.15', '30');

建立实体类

@Table(name = "tabple_emp")public class Employee {     //   @Transient当数据库里面没有某个字段的时候可以用此注解    private Integer empId;    //    当数据字段和实体类字段不一致时可以用该字段    @Column(name = "emp_name")    private String empName;    private Double empSalary;    private Integer empAge;    get,set方法省略.......    }

建立mapper的dao层

package com.yang.mapper.dao;

import com.yang.mapper.entity.Employee;import tk.mybatis.mapper.common.Mapper;

/** * 继承Mapper * */public interface EmployeeMapper extends Mapper<Employee> {

}

简单的测试

package com.yang.mapper.services;

import com.yang.mapper.entity.Employee;import org.apache.ibatis.session.RowBounds;import org.junit.Test;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;import tk.mybatis.mapper.entity.Example;

import java.util.List;

import static org.junit.Assert.*;

public class EmployeeServiceTest {

    private ApplicationContext iocContainer = new ClassPathXmlApplicationContext("spring-context.xml");    private EmployeeService employeeService = iocContainer.getBean(EmployeeService.class);

    @Test    public void testSelectOne() {

        //1.创建封装查询条件的实体类对象        Employee employeeQueryCondition = new Employee(null, "bob", 5560.11, null);

        //2.执行查询        Employee employeeQueryResult = employeeService.getOne(employeeQueryCondition);

        //3.打印        System.out.println(employeeQueryResult);    }    @Test    public void testSelectByPrimaryKey() {

        //1.提供id值        Integer empId = 3;

        //2.执行根据主键进行的查询        Employee employee = employeeService.getEmployeeById(empId);

        //3.打印结果        System.out.println(employee);

    }

    @Test    public void testExistsWithPrimaryKey() {

        //1.提供主键值        Integer empId = 33;

        //2.执行查询        boolean exists = employeeService.isExists(empId);

        //3.打印结果        System.out.println(exists);

    }

    @Test    public void testInsert() {

        //1.创建实体类对象封装要保存到数据库的数据        Employee employee = new Employee(null, "emp03", 3000.00, 23);

        //2.执行插入操作        employeeService.saveEmployee(employee);

        //3.获取employee对象的主键字段值        Integer empId = employee.getEmpId();        System.out.println("empId="+empId);

    }

    @Test    public void testInsertSelective() {

        //1.创建实体类对象封装要保存到数据库的数据        Employee employee = new Employee(null, "emp04", null, 23);

        //2.执行插入操作        employeeService.saveEmployeeSelective(employee);

    }

    @Test    public void testUpdateByPrimaryKeySelective() {

        //1.创建用于测试的实体类        Employee employee = new Employee(7, "empNewName", null, null);

        //2.执行更新        employeeService.updateEmployeeSelective(employee);

    }

    @Test    public void testDelete() {

        //1.声明实体类变量作为查询条件        Employee employee = null;

        //2.执行删除        employeeService.removeEmployee(employee);

    }

    @Test    public void testDeleteByPrimaryKey() {

        //1.提供主键值        Integer empId = 13;

        //2.执行删除        employeeService.removeEmployeeById(empId);

    }

    @Test    public void testSelectByExample() {

        //目标:WHERE (emp_salary>? AND emp_age) OR (emp_salary AND emp_age>?)        //1.创建Example对象        Example example = new Example(Employee.class);

        //***********************        //i.设置排序信息        example.orderBy("empSalary").asc().orderBy("empAge").desc();

        //ii.设置“去重”        example.setDistinct(true);

        //iii.设置select字段        example.selectProperties("empName","empSalary");

        //***********************

        //2.通过Example对象创建Criteria对象        Example.Criteria criteria01 = example.createCriteria();        Example.Criteria criteria02 = example.createCriteria();

        //3.在两个Criteria对象中分别设置查询条件        //property参数:实体类的属性名        //value参数:实体类的属性值        criteria01.andGreaterThan("empSalary", 3000)                .andLessThan("empAge", 25);

        criteria02.andLessThan("empSalary", 5000)                .andGreaterThan("empAge", 30);

        //4.使用OR关键词组装两个Criteria对象        example.or(criteria02);

        //5.执行查询        List empList = employeeService.getEmpListByExample(example);for (Employee employee : empList) {            System.out.println(employee);        }    }@Testpublic void testSelectByRowBounds() {int pageNo = 3;int pageSize = 5;int index = (pageNo - 1) * pageSize;        RowBounds rowBounds = new RowBounds(index, pageSize);        List empList = employeeService.getEmpListByRowBounds(rowBounds);for (Employee employee : empList) {            System.out.println(employee);        }    }}

当数据库为空时可以加个注解@Id

package com.yang.mapper.entity;

import javax.persistence.Column;import javax.persistence.Id;import javax.persistence.Table;//指定数据库表名@Table(name = "tabple_emp")public class Employee {    @Id    @GeneratedValue(strategy = GenerationType.IDENTITY)    private Integer empId;    private Integer empId;//    当数据字段和实体类字段不一致时可以用该字段    @Column(name = "emp_name")    private String empName;    private Double empSalary;    private Integer empAge;

通用mapper_通用Mapper快速开发,搭建项目相关推荐

  1. 10个Spring Boot快速开发的项目,接私活利器(快速、高效)

    本文为大家精选了 码云 上优秀的 Spring Boot 语言开源项目,涵盖了企业级系统框架.文件文档系统.秒杀系统.微服务化系统.后台管理系统等,希望能够给大家带来一点帮助:) 1.项目名称:分布式 ...

  2. 11个springboot快速开发的项目,接私活利器

    转自公众号:码云Gitee 本文为大家精选了 码云 上优秀的 Spring Boot 语言开源项目,涵盖了企业级系统框架.文件文档系统.秒杀系统.微服务化系统.后台管理系统等,希望能够给大家带来一点帮 ...

  3. SpringBoot(一)_快速实战搭建项目

    现在在学习springboot 相关的知识,感觉真的很好用,用idea 进行开发,根据慕课网和纯洁的微笑的课程.进行总结下. 使用idea创建springboot项目 (1)单击 File | New ...

  4. Spring Boot + Security + MyBatis + Thymeleaf + Activiti 快速开发平台项目

    项目介绍 Spring Boot + Security + MyBatis + Thymeleaf + Activiti 快速开发平台 基于 Layui 的后台管理系统模板,扩展 Layui 原生 U ...

  5. Spring Boot + Security + Thymeleaf + Activiti 快速开发平台项目

    点击关注公众号,实用技术文章及时了解 项目介绍 Spring Boot + Security + MyBatis + Thymeleaf + Activiti 快速开发平台 基于 Layui 的后台管 ...

  6. 基于Element,快速开发Vue项目

    Element,是一套基于Vue2.0的UI组件库,利用它,我们可以可以快速开发我们的Vue项目. 官网网址:http://element-cn.eleme.io/#/zh-CN 闲话不多说,我们将基 ...

  7. 推荐 3 个快速开发平台 项目经验又有着落了

    微信搜索逆锋起笔关注后回复编程pdf 领取编程大佬们所推荐的 23 种编程资料! 经常性逛github,发现了一些优秀的开源项目,其中的框架及代码非常不错,现在给大家推荐三个快速开发平台. 第一个就是 ...

  8. mysql通用mapper_通用mapper的介绍和入门使用

    通用mapper的介绍和入门使用 简介 通用Mapper都可以极大的方便开发人员.可以随意的按照自己的需要选择通用方法,还可以很方便的开发自己的通用方法. 极其方便的使用MyBatis单表的增删改查. ...

  9. mysql通用mapper_通用Mapper(Mybatis)

    1.Mapper的简单介绍 2.Mapper的作用 通用Mapper可以通过Mybatis的拦截器原理,动态的帮我们实现单表的增删改查功能,大大降低了我们的开发成本,减少了我们的工作量. 3.Mapp ...

最新文章

  1. (NO.00003)iOS游戏简单的机器人投射游戏成形记(二)
  2. 汉字转拼音php代码函数,php中将汉字转换成拼音的函数代码
  3. 忘记手机绑定过的UC/交易猫账号怎么找回
  4. 2015-12-03 AD中用户属性Lastlogon与LastlogonTimeStamp的区别
  5. html5实现圆圈里带一个三角形,CSS制作箭头图标代码(圆,三角形,椭圆)c
  6. hbase scan超时设置_HBase学习之路 (六)过滤器
  7. 【NLP】毕设学习笔记(四)bert相关知识点
  8. mvc5控制器修改html,ASP.NET MVC Razor:如何在控制器动作中呈现Razor局部视图的HTML...
  9. RTP/RTCP协议与RTSP协议
  10. Math工具类的使用
  11. HPC+AI融合发展的挑战和应对方法探讨
  12. MFC界面库BCGControlBar Pro for MFC v33.1 - 更适配Windows 11
  13. 备考进行时!2020年中级通信工程师传输与接入(无线)考试大纲
  14. c++ 各种求min/max方法效率测试
  15. 雷达水位计的工作原理及安装维护注意事项
  16. 一张图搞懂CPU、OpenGL/DirectX、显卡驱动和GPU之间的关系
  17. photoshop油画滤镜使用和案例教程
  18. keil 报错解决 Loading PDSC Debug Description failed forSTMicroelectronics STM32xxxx
  19. 拦截来电(来电挂断)
  20. java火柴游戏_Java课程设计—拿火柴小游戏

热门文章

  1. Unity之如何从fbx提取Animation clip文件
  2. 原生JS事件中,return false 和 preventDefault() 的区别
  3. 使用ctime.h头文件来控制程序延时秒数
  4. JAVA通过调用数据库函数调用存储过程
  5. 移动硬盘WINPE启动盘安装方法图解
  6. [C11] 推荐系统(Recommender Systems)
  7. StackExchange.Redis 官方文档(五) Keys, Values and Channels
  8. Python入门:生成器并行(协程)
  9. 在网页中嵌入Base64编码文件
  10. 《恋上数据结构第1季》二叉树代码实现