1.CRUD的含义

CRUD是指在做计算处理时的增加(Create)、读取(Retrieve)(重新得到数据)、更新(Update)和删除(Delete)几个单词的首字母简写。主要被用在描述软件系统中数据库或者持久层的基本操作功能。

2.URL的概念及含义

URL:Uniform Resource Locator 统一资源定位符。它是可以唯一标识一个资源的位置。
它的写法:
http://localhost:8080/mybatisserver/demo1Servlet
协议 主机    端口    URI

URI:Uniform Resource Identifier 统一资源标识符。它是在应用中可以唯一定位一个资源的。

3.Mybatis的CRUD操作

数据库表结构图:

数据库对应的实体类User.java

package domain;

import java.io.Serializable;
import java.util.Date; /**
* 数据库表对应的实体类
*/
public class User implements Serializable {
//实体类的成员变量名称应该与数据库中的列名一致
private Integer id;
private String username;
private Date birthday;
private String sex;
private String address; public Integer getId() {
return id;
} public void setId(Integer id) {
this.id = id;
} public String getUsername() {
return username;
} public void setUsername(String username) {
this.username = username;
} public Date getBirthday() {
return birthday;
} public void setBirthday(Date birthday) {
this.birthday = birthday;
} public String getSex() {
return sex;
} public void setSex(String sex) {
this.sex = sex;
} public String getAddress() {
return address;
} public void setAddress(String address) {
this.address = address;
} @Override
public String toString() {
return "User{" +
"id=" + id +
", username='" + username + '\'' +
", birthday=" + birthday +
", sex='" + sex + '\'' +
", address='" + address + '\'' +
'}';
}
}

IUserDao.java数据库表操作实现接口

package dao;

import domain.QueryConditon;
import domain.User;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.junit.Test; import java.util.List; /**
*
*/
public interface IUserDao {
/**
* 查询所有
* @return
*/
//注解模式
@Select("select *from user")
List<User> findAll(); /**
* 保存用户
* @param user
*/
@Insert("insert into user(username,address,sex,birthday)values(#{username},#{address},#{sex},#{birthday})")
void saveUser(User user); /**
* 更新操作
* @param user
*/
@Update("update user set username=#{username},address=#{address},sex=#{sex},birthday=#{birthday} where id=#{id}")
void updateUser(User user); /**
* 删除操作
* @param userId 用户id
*/
@Delete("delete from user where id=#{userId}")
void deleteUser(Integer userId); /**
* 根据用户id查询
* @param userId 用户id
*/
@Select("select * from user where id=#{userId}")
User queryById(Integer userId); /**
* 根据用户名进行模糊查询
* @param username 用户名
* @return 查询结果
*/
@Select("select *from user where username like #{username} ")
List<User> queryByName(String username); /**
* 获取用户的总记录数
* @return
*/
@Select("select count(id) from user")
int queryTotalCount(); /**
* 根据查询条件对象(由实体类生成)进行查询
* @param queryConditon 查询条件
* @return
*/
@Select("select *from user where username like #{user.username} and sex like #{user.sex}")
List<User> queryByQueryConditionObject(QueryConditon queryConditon);
}

测试类

package test;

import dao.IUserDao;
import domain.QueryConditon;
import domain.User;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Test; import java.io.InputStream;
import java.util.Date;
import java.util.List; public class MybatisTest01 { private InputStream in;
private SqlSession sqlSession;
private IUserDao userDao; /**
* 初始化MyBatis
* @throws Exception
*/
public void initMyBatis() throws Exception{
//1.读取配置文件
in = Resources.getResourceAsStream("SqlMapConfig.xml");
//2.创建SqlSessionFactory
SqlSessionFactoryBuilder builder=new SqlSessionFactoryBuilder(); //创建SqlSessionFactory的构建者builder
SqlSessionFactory factory=builder.build(in); //利用构建者builder创建SqlSessionFactory
//3.使用工厂生产SqlSession对象
sqlSession = factory.openSession();
//4.使用SqlSessions对象创建Dao接口的代理对象
userDao = sqlSession.getMapper(IUserDao.class);
} /**
* 释放资源
* @throws Exception
*/
public void destroy() throws Exception{
sqlSession.commit();//提交事务
sqlSession.close();
in.close();
} /**
* 查询所有
*/
@Test
public void testQueryAll() throws Exception{ initMyBatis();
//5.使用代理对象执行方法
List<User> users = userDao.findAll();
for (User user : users) {
System.out.println(user);
}
destroy();
} /**
* 测试保存操作
* @throws Exception
*/
@Test
public void testSave() throws Exception{
User user=new User();
user.setUsername("lucky");
user.setAddress("天台");
user.setBirthday(new Date());
user.setSex("男"); initMyBatis();
//5.使用代理对象执行方法
userDao.saveUser(user); destroy();
} /**
* 测试更新操作
* @throws Exception
*/
@Test
public void testUpdate() throws Exception{
User user=new User();
user.setId(49);
user.setUsername("lucky");
user.setAddress("浙江天台");
user.setBirthday(new Date());
user.setSex("男"); initMyBatis();
//5.使用代理对象执行方法
userDao.updateUser(user); destroy();
} /**
* 测试更新操作
* @throws Exception
*/
@Test
public void testDelete() throws Exception{ initMyBatis();
//5.使用代理对象执行方法
userDao.deleteUser(48);
destroy();
} /**
* 测试查询一个的方法
* @throws Exception
*/
@Test
public void testQueryOne()throws Exception{
initMyBatis();
//5.使用代理对象执行方法
User user=userDao.queryById(49);
System.out.println(user);
destroy();
} /**
* 模糊查询
* @throws Exception
*/
@Test
public void testQueryByName()throws Exception{
initMyBatis();
//5.使用代理对象执行方法
List<User> users = userDao.queryByName("%ck%");
for (User user : users) {
System.out.println(user);
}
destroy();
} /**
* 查询总记录数
* @throws Exception
*/
@Test
public void testQueryTotalCount()throws Exception{
initMyBatis();
//5.使用代理对象执行方法
int totalCount = userDao.queryTotalCount();
System.out.println(totalCount);
destroy();
} /**
* 根据查询条件实体类对象进行查询
* @throws Exception
*/
@Test
public void testQueryConditionObject()throws Exception{
initMyBatis();
QueryConditon queryConditon=new QueryConditon(); //创建查询条件实体类
User user=new User();
user.setUsername("%王%"); //查询条件1:名字包含王
user.setSex("女"); //性别为女
queryConditon.setUser(user); //将查询条件封装到查询条件实体类QueryConditon中
//5.使用代理对象执行方法
List<User> users = userDao.queryByQueryConditionObject(queryConditon);
for (User user1 : users) {
System.out.println(user1);
}
destroy();
} }

4.Mybatis连接池

(1)连接池综述:
  我们在实际开发中都会使用连接池。
  因为它可以减少我们获取连接所消耗的时间。


(2)mybatis中的连接池
mybatis连接池提供了3种方式的配置:
配置的位置:
  主配置文件SqlMapConfig.xml中的dataSource标签,type属性就是表示采用何种连接池方式。
  type属性的取值:
    POOLED    采用传统的javax.sql.DataSource规范中的连接池,mybatis中有针对规范的实现
    UNPOOLED    采用传统的获取连接的方式,虽然也实现Javax.sql.DataSource接口,但是并没有使用池的思想。
    JNDI                采用服务器提供的JNDI技术实现,来获取DataSource对象,不同的服务器所能拿到DataSource是不一样。
                 注意:如果不是web或者maven的war工程,是不能使用的。
            我们课程中使用的是tomcat服务器,采用连接池就是dbcp连接池。

05 Mybatis的CRUD操作和Mybatis连接池的更多相关文章

  1. 【MyBatis】MyBatis实现CRUD操作

    1.实现基本CRUD功能 使用MyBatis对数据完整的操作,也就是CRUD功能的实现.根据之前的内容,要想实现CRUD,只需要进行映射文件的配置. 范例:修改EmpMapper.xml文件,实现CR ...

  2. mybatis深入之动态查询和连接池介绍

    mybatis深入之动态查询和连接池介绍 一.mybatis条件查询 在mybatis前述案例中,我们的查询条件都是确定的.但在实际使用的时候,我们的查询条件有可能是动态变化的.例如,查询参数为一个u ...

  3. java操作mongodb(连接池)(转)

    原文链接: java操作mongodb(连接池) Mongo的实例其实就是一个数据库连接池,这个连接池里默认有10个链接.我们没有必要重新实现这个链接池,但是我们可以更改这个连接池的配置.因为Mong ...

  4. Java操作数据库——使用连接池连接数据库

    Java操作数据库——使用连接池连接数据库 摘要:本文主要学习了如何使用JDBC连接池连接数据库. 传统方式和连接池方式 传统方式的步骤 使用传统方式在Java中使用JDBC连接数据库,完成一次数据库 ...

  5. 数据库CRUD操作以及MyBatis的配置使用

    • 业务字段设计 • 数据库创建 • CRUD操作 • MyBatis集成 • 注解和XML定义 • ViewObject和DateTool • 首页开发     • 业务字段设计 实体: name: ...

  6. Mybatis基于代理Dao实现CRUD操作 及 Mybatis的参数深入

    Mybatis基于代理Dao实现CRUD操作 使用要求: 1.持久层接口和持久层接口的映射配置必须在相同的包下 2.持久层映射配置中mapper标签的namespace属性取值必须是持久层接口的全限定 ...

  7. Spring boot 入门四:spring boot 整合mybatis 实现CRUD操作

    开发环境延续上一节的开发环境这里不再做介绍 添加mybatis依赖 <dependency> <groupId>org.mybatis.spring.boot</grou ...

  8. SpringBoot入门篇--整合mybatis+generator自动生成代码+druid连接池+PageHelper分页插件

    原文链接 我们这一篇博客讲的是如何整合Springboot和Mybatis框架,然后使用generator自动生成mapper,pojo等文件.然后再使用阿里巴巴提供的开源连接池druid,这个连接池 ...

  9. java操作redis redis连接池

    redis作为缓存型数据库,越来越受到大家的欢迎,这里简单介绍一下java如何操作redis. 1.java连接redis java通过需要jedis的jar包获取Jedis连接. jedis-2.8 ...

随机推荐

  1. [ARIA] aria-describedby & aria-labelledby

    When to use describedby: For example you have a close button: <button aria-describedby="clos ...

  2. asp.net大附件上传,支持断点续传

    以ASP.NET Core WebAPI 作后端 API ,用 Vue 构建前端页面,用 Axios 从前端访问后端 API ,包括文件的上传和下载. 准备文件上传的API #region 文件上传  ...

  3. Using HAProxy as an API Gateway, Part 3 [Health Checks]

    转自:https://www.haproxy.com/blog/using-haproxy-as-an-api-gateway-part-3-health-checks/ Achieving high ...

  4. [RN] React Native 使用 react-native-vector-icons 图标显示问号

    我在第一次使用 react-native-vector-icons 时图标显示问号 后来在网上查了很多文章,发现原因有两个 1)安装完 react-native-vector-icons 后,没有li ...

  5. [USACO06FEB] Stall Reservations 贪心

    [USACO06FEB] Stall Reservations 贪心 \(n\)头牛,每头牛占用时间区间\([l_i,r_i]\),一个牛棚每个时间点只能被一头牛占用,问最少新建多少个牛棚,并且每头牛 ...

  6. 洛谷 P3905 道路重建 题解

    P3905 道路重建 题目描述 从前,在一个王国中,在\(n\)个城市间有\(m\)条道路连接,而且任意两个城市之间至多有一条道路直接相连.在经过一次严重的战争之后,有\(d\)条道路被破坏了.国王想 ...

  7. Theano安装笔记

    由于实验需要,近三个月来,安装过十几次Theano,基本上每次都是从最基本的nvidia driver装起.总结一些粗浅的安装心得. GPU:Nvidia K40, M40, M60 软件环境:Unb ...

  8. rust-vmm 学习

    V0.1.0 feature base knowledge: Architecture of the Kernel-based Virtual Machine (KVM) 用rust-vmm打造未来的 ...

  9. Promise链式调用 终止或取消

    Promise回调分两种方法,then成功,catch失败 let promise = new Promise(function(resolve, reject){ resolve('第一次成功') ...

  10. Android中创建自定义控件

    1.创建一个TitleLayout继承LinearLayout: //创建自定义控件 public class TitleLayout extends LinearLayout { private f ...