1。dao层接口引入

 package com.baidu.www.mplus.mapper;

 import com.baidu.www.mplus.bean.Employee;
import com.baomidou.mybatisplus.core.mapper.BaseMapper; /**
* @author liuyangos8888
* <p>
* 抽象接口继承B
* 实际的实现都在
* r
*/
public interface EmployeeMapper extends BaseMapper<Employee> { }

这里主要是继承MybatisPlus 的BaseMapper抽象类,实现其内部拥有的通用的CRUD(增删改查),不用写xml文档,自动实现环境配置

源码

 /*
* Copyright (c) 2011-2020, hubin (jobob@qq.com).
* <p>
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy of
* the License at
* <p>
* http://www.apache.org/licenses/LICENSE-2.0
* <p>
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
*/
package com.baomidou.mybatisplus.core.mapper; import java.io.Serializable;
import java.util.Collection;
import java.util.List;
import java.util.Map; import org.apache.ibatis.annotations.Param; import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Constants; /* :`
.:,
:::,,.
:: `::::::
::` `,:,` .:`
`:: `::::::::.:` `:';,`
::::, .:::` `@++++++++:
`` :::` @+++++++++++#
:::, #++++++++++++++`
,: `::::::;'##++++++++++
.@#@;` ::::::::::::::::::::;
#@####@, :::::::::::::::+#;::.
@@######+@:::::::::::::. #@:;
, @@########':::::::::::: .#''':`
;##@@@+:##########@::::::::::: @#;.,:.
#@@@######++++#####'::::::::: .##+,:#`
@@@@@#####+++++'#####+::::::::` ,`::@#:`
`@@@@#####++++++'#####+#':::::::::::@.
@@@@######+++++''#######+##';::::;':,`
@@@@#####+++++'''#######++++++++++`
#@@#####++++++''########++++++++'
`#@######+++++''+########+++++++;
`@@#####+++++''##########++++++,
@@######+++++'##########+++++#`
@@@@#####+++++############++++;
;#@@@@@####++++##############+++,
@@@@@@@@@@@###@###############++'
@#@@@@@@@@@@@@###################+:
`@#@@@@@@@@@@@@@@###################'`
:@#@@@@@@@@@@@@@@@@@##################,
,@@@@@@@@@@@@@@@@@@@@################;
,#@@@@@@@@@@@@@@@@@@@##############+`
.#@@@@@@@@@@@@@@@@@@#############@,
@@@@@@@@@@@@@@@@@@@###########@,
:#@@@@@@@@@@@@@@@@##########@,
`##@@@@@@@@@@@@@@@########+,
`+@@@@@@@@@@@@@@@#####@:`
`:@@@@@@@@@@@@@@##@;.
`,'@@@@##@@@+;,`
``...`` _ _ /_ _ _/_. ____ / _
/ / //_//_//_|/ /_\ /_///_/_\ Talk is cheap. Show me the code.
_/ /
*/ /**
* <p>
* Mapper 继承该接口后,无需编写 mapper.xml 文件,即可获得CRUD功能
* </p>
* <p>
* 这个 Mapper 支持 id 泛型
* </p>
*
* @author hubin
* @since 2016-01-23
*/
public interface BaseMapper<T> { /**
* <p>
* 插入一条记录
* </p>
*
* @param entity 实体对象
*/
int insert(T entity); /**
* <p>
* 根据 ID 删除
* </p>
*
* @param id 主键ID
*/
int deleteById(Serializable id); /**
* <p>
* 根据 columnMap 条件,删除记录
* </p>
*
* @param columnMap 表字段 map 对象
*/
int deleteByMap(@Param(Constants.COLUMN_MAP) Map<String, Object> columnMap); /**
* <p>
* 根据 entity 条件,删除记录
* </p>
*
* @param queryWrapper 实体对象封装操作类(可以为 null)
*/
int delete(@Param(Constants.WRAPPER) Wrapper<T> queryWrapper); /**
* <p>
* 删除(根据ID 批量删除)
* </p>
*
* @param idList 主键ID列表(不能为 null 以及 empty)
*/
int deleteBatchIds(@Param(Constants.COLLECTION) Collection<? extends Serializable> idList); /**
* <p>
* 根据 ID 修改
* </p>
*
* @param entity 实体对象
*/
int updateById(@Param(Constants.ENTITY) T entity); /**
* <p>
* 根据 whereEntity 条件,更新记录
* </p>
*
* @param entity 实体对象 (set 条件值,不能为 null)
* @param updateWrapper 实体对象封装操作类(可以为 null,里面的 entity 用于生成 where 语句)
*/
int update(@Param(Constants.ENTITY) T entity, @Param(Constants.WRAPPER) Wrapper<T> updateWrapper); /**
* <p>
* 根据 ID 查询
* </p>
*
* @param id 主键ID
*/
T selectById(Serializable id); /**
* <p>
* 查询(根据ID 批量查询)
* </p>
*
* @param idList 主键ID列表(不能为 null 以及 empty)
*/
List<T> selectBatchIds(@Param(Constants.COLLECTION) Collection<? extends Serializable> idList); /**
* <p>
* 查询(根据 columnMap 条件)
* </p>
*
* @param columnMap 表字段 map 对象
*/
List<T> selectByMap(@Param(Constants.COLUMN_MAP) Map<String, Object> columnMap); /**
* <p>
* 根据 entity 条件,查询一条记录
* </p>
*
* @param queryWrapper 实体对象
*/
T selectOne(@Param(Constants.WRAPPER) Wrapper<T> queryWrapper); /**
* <p>
* 根据 Wrapper 条件,查询总记录数
* </p>
*
* @param queryWrapper 实体对象
*/
Integer selectCount(@Param(Constants.WRAPPER) Wrapper<T> queryWrapper); /**
* <p>
* 根据 entity 条件,查询全部记录
* </p>
*
* @param queryWrapper 实体对象封装操作类(可以为 null)
*/
List<T> selectList(@Param(Constants.WRAPPER) Wrapper<T> queryWrapper); /**
* <p>
* 根据 Wrapper 条件,查询全部记录
* </p>
*
* @param queryWrapper 实体对象封装操作类(可以为 null)
*/
List<Map<String, Object>> selectMaps(@Param(Constants.WRAPPER) Wrapper<T> queryWrapper); /**
* <p>
* 根据 Wrapper 条件,查询全部记录
* 注意: 只返回第一个字段的值
* </p>
*
* @param queryWrapper 实体对象封装操作类(可以为 null)
*/
List<Object> selectObjs(@Param(Constants.WRAPPER) Wrapper<T> queryWrapper); /**
* <p>
* 根据 entity 条件,查询全部记录(并翻页)
* </p>
*
* @param page 分页查询条件(可以为 RowBounds.DEFAULT)
* @param queryWrapper 实体对象封装操作类(可以为 null)
*/
IPage<T> selectPage(IPage<T> page, @Param(Constants.WRAPPER) Wrapper<T> queryWrapper); /**
* <p>
* 根据 Wrapper 条件,查询全部记录(并翻页)
* </p>
*
* @param page 分页查询条件
* @param queryWrapper 实体对象封装操作类
*/
IPage<Map<String, Object>> selectMapsPage(IPage<T> page, @Param(Constants.WRAPPER) Wrapper<T> queryWrapper);
}

多数方法都是以命名为其真诚的语句使用方式,所以可以直接调用,很多大企业,也是做了自己企业的类似封装。所以学会这套框架,就是你学会了大企业接口调用的方式。

2.测试类编写

(1)环境测试,测试配置文件是否有效,环境是否搭建成功

 import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.SQLException; /**
* test
*/
public class TestMp {
private ApplicationContext iocContext = new ClassPathXmlApplicationContext("applicationContext.xml"); @Test
public void context() throws SQLException { DataSource ds = iocContext.getBean("dataSource", DataSource.class);
Connection conn = ds.getConnection();
System.out.println(conn); }
}

(2)CRUD调用测试

 import com.baidu.www.mplus.bean.Employee;
import com.baidu.www.mplus.mapper.EmployeeMapper;
import com.google.gson.Gson;
import org.apache.log4j.Logger;
import org.apache.log4j.spi.LoggerFactory;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; import java.sql.SQLException;
import java.util.List; /**
* test
*/
public class TestCRUD { private ApplicationContext iocContext = new ClassPathXmlApplicationContext("applicationContext.xml"); private EmployeeMapper employeeMapper = iocContext.getBean("employeeMapper",EmployeeMapper.class); private final static Logger logger = Logger.getLogger(TestCRUD.class); Gson gson = new Gson(); /**
* 所有员工列表
*
* @throws SQLException
*/
@Test
public void list() throws SQLException { List<Employee> employeeList = employeeMapper.selectList(null); if (!employeeList.isEmpty()) {
logger.info("所有员工:"+gson.toJson(employeeList));
}
} /**
* 添加用户
* @throws SQLException
*/
@Test
public void add() throws SQLException { Employee employee = new Employee(); employee.setLastName("Betty");
employee.setAge(12);
employee.setEmail("betty@163.com");
employee.setGender(1); Integer result = employeeMapper.insert(employee); if (result!=null||result>0) {
logger.info("+++++++++++++++++添加成功+++++");
}
} /**
* 修改用户
* @throws SQLException
*/
@Test
public void update() throws SQLException { Employee employee = new Employee(); employee.setLastName("Marry");
employee.setAge(12);
employee.setEmail("marry@163.com");
employee.setGender(1); Integer result = employeeMapper.updateById(employee); if (result!=null||result>0) {
logger.info("++++++++++++++++修改成功+++++");
}
} @Test
public void selectById() throws SQLException { Employee employee = employeeMapper.selectById(1); if (employee!=null) {
logger.info("++一个员工信息+++++"+gson.toJson(employee));
}
} }

注意:

CRUD测试,导入了相关jar包,为了显示方便

         <dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.3.1</version>
</dependency>

3.可能遇到两个问题,就是数据库名称和实体类对应字段名称不匹配问题

需要修改加入注解@TableName(value = "tbl_employee")和字段@TableField(value = "last_name"),源码如下,同时添加了自动生产ID功能@TableId(type = IdType.AUTO)

源码

 package com.baidu.www.mplus.bean;

 import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName; /**
*
* @author liuyangos8888
*
* 实体类
* 建议使用封装类型String,Integer,方便框架判空
*
*/
@TableName(value = "tbl_employee")
public class Employee{ /**
* 字段的ID
*/
@TableId(type = IdType.AUTO)
private Integer id ; /**
* 名字
*/
@TableField(value = "last_name")
private String lastName; /**
* 邮箱
*/
private String email ; /**
* 性别
*/
private Integer gender ; /**
* 年龄
*/
private Integer age ; public Integer getId() {
return id;
} public void setId(Integer id) {
this.id = id;
} public String getLastName() {
return lastName;
} public void setLastName(String lastName) {
this.lastName = lastName;
} public String getEmail() {
return email;
} public void setEmail(String email) {
this.email = email;
} public Integer getGender() {
return gender;
} public void setGender(Integer gender) {
this.gender = gender;
} public Integer getAge() {
return age;
} public void setAge(Integer age) {
this.age = age;
} @Override
public String toString() {
return "Employee{" +
"id=" + id +
", lastName='" + lastName + '\'' +
", email='" + email + '\'' +
", gender=" + gender +
", age=" + age +
'}';
}
}

最新文章

  1. pip UnicodeDecodeError: &#39;ascii&#39; codec can&#39;t decode byte
  2. DOM兼容
  3. C++11带来的优雅语法
  4. .NET中的流
  5. 实现jQuery扩展总结
  6. Linux中ftp不能上传文件/目录的解决办法
  7. leetcode:Reverse Linked List
  8. OSI七层协议
  9. hdu2457:DNA repair
  10. Uva 315 Network 判断割点
  11. (转)webservice 测试窗体只能用于来自本地计算机的请求
  12. CryEngine3教程合辑
  13. arc engine - IName
  14. Jlink仿真器下载程序时出现Invalid ROM table!
  15. Android中通过代码获取arrays.xml文件中的数据
  16. 知识小罐头03(idea+maven+部署war包到tomcat 上)
  17. shell脚本实现git和svn统计log代码行
  18. Jprofiler监控远程jvm
  19. Html5、css、JavaScript基础
  20. 挂在光盘出现写保护mount: block device /dev/sr0 is write-protected, mounting read-only

热门文章

  1. ubuntu系统ssh遇到port 22:No route to host问题
  2. JS编程题练习
  3. how2j网站前端项目——天猫前端(第一次)学习笔记1
  4. wget: command not found
  5. WEB框架之Django实现分页功能
  6. css背景图充满屏幕
  7. UVA-1364.Knights of the Round Table 无向图BCC
  8. 谁说delphi没有IOCP库,delphi新的IOCP类库,开源中
  9. the example of dlsym
  10. How to convert a PDF file to JPEGs using PHP