SSM框架学习之MyBatis(二)——MyBatis基本操作实现增删改查
一、创建数据库
1、建“mybatis_test”数据库
2、建“customers”表
可直接在数据库里建表或者在代码中建表。
二、创建实体类(称为javabean/entity/domain)
1、建Customer实体类
2、编写实体类的代码
package com.ssm.mybatis.entity;
/**
* customer实体类
*/
public class Customer {
private int id;
private String name;
private int age;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
三、创建Mapper文件
1、在resources下新建customerMapper.xml文件
映射文件存放sql操作语句。
2、编写映射文件CustomerMapper.xml
- 为域名空间
- 操作数据库的SQL语句写在此处。
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<!--定义域名空间-->
<mapper namespace="CustomerMapper">
</mapper>
四、创建mybatis配置文件
1、在resources下新建xml文件
2、编写mybatis-config.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<!-- mybatis的配置文件 -->
<configuration>
<!--配置开发环境-->
<properties>
<property name="driver" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/mybatis_test"/>
<property name="username" value="root"/>
<property name="password" value=""/>
</properties>
<environments default="development">
<environment id="development">
<transactionManager type="JDBC"/>
<dataSource type="POOLED">
<!-- 链接的驱动 -->
<property name="driver" value="${driver}"/>
<!-- 链接的路径 -->
<property name="url" value="${url}"/>
<!-- 数据库名-->
<property name="username" value="${username}"/>
<!-- 密码 -->
<property name="password" value="${password}"/>
</dataSource>
</environment>
</environments>
<!--填写相关配置文件-->
<mappers>
<mapper resource="CustomerMapper.xml"/>
</mappers>
</configuration>
注意:
- 若为UNPOOLED,mybatis会为每一个数据库操作创建一个新的连接,并关闭它。该方式适用于只有小规模数量并发用户的简单应用程序上。
- 若为POOLED,mybatis会创建一个数据库连接池,连接池中的一个连接将会被用作数据库的操作,一旦数据库操作完成,mybatis会将此连接返回给连接池。在开发或测试环境中,经常使用此方式。
- 若为JNDI,mybatis从在应用服务器向配置好的JNDI数据源dataSource获取数据库连接。在生产环境中,优先考虑这种方式。
五、增删改查
在测试类中进行单元测试,创建测试类TestCRUD.java
1、增
编写映射文件CustomerMapper.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<!--定义域名空间-->
<mapper namespace="CustomerMapper">
<!--插入操作-->
<insert id="insert">
insert into customers(id,name,age) values (#{id}, #{name},#{age})
</insert>
</mapper>
编写测试类insert()
package com.ssm.mybatis.test;
import com.ssm.mybatis.entity.Customer;
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;
/**
* 测试增删改查
*/
public class TestCRUD {
//增加
@Test
public void insert() throws Exception {
//制定配置文件路径
String resource ="mybatis-config.xml";
//加载文件
//Resources.getResourceAsStream(resource):得到资源文件
InputStream inputStream= Resources.getResourceAsStream(resource);
//使用builder模式创建会话工厂,相当于链接池
SqlSessionFactory sf=new SqlSessionFactoryBuilder().build(inputStream);
//通过sf打开会话,相当于打开链接
SqlSession s=sf.openSession();
Customer customer=new Customer();
customer.setId(1);
customer.setName("jerry");
customer.setAge(12);
//CustomerMapper.insert:CustomerMapper位映射文件的域名
s.insert("CustomerMapper.insert",customer);
//关闭
s.commit();
s.close();
}
}
查看数据库,可观察到新增一条记录
2、查
编写映射文件CustomerMapper.xml
<!--查询一个信息操作-->
<!--查询会返回一个javabean,定义查询返回的类型resultType="完整路径.类名",此处封装为Customer类-->
<select id="findbyid" resultType="com.ssm.mybatis.entity.Customer">
select * from customers where id=#{id}
</select>
注意:
查询会返回一个javabean,需要定义返回类型。
编写测试类findbyid()
//查询
@Test
public void findbyid() throws Exception {
String resource ="mybatis-config.xml";
InputStream inputStream=Resources.getResourceAsStream(resource);
SqlSessionFactory sf=new SqlSessionFactoryBuilder().build(inputStream);
SqlSession s=sf.openSession();
Customer customer=s.selectOne("CustomerMapper.findbyid",1);
System.out.println(customer.getName());
s.commit();
s.close();
}
查看控制台,输出“jerry”
3、改
编写映射文件CustomerMapper.xml
<!--更新操作-->
<update id="updatebyid">
update customers set name =#{name} where id=#{id}
</update>
编写测试类updatebyid()
//更新
@Test
public void updatebyid() throws Exception {
String resource="mybatis-config.xml";
InputStream inputStream=Resources.getResourceAsStream(resource);
SqlSessionFactory sf=new SqlSessionFactoryBuilder().build(inputStream);
SqlSession s=sf.openSession();
Customer customer=s.selectOne("CustomerMapper.findbyid",1);
customer.setName("alice");
//customer.setAge(24);
s.update("CustomerMapper.updatebyid",customer);
s.commit();
s.close();
}
更新操作思路:先查询到需要更改的个体,在通过更新个体元素达到更改目的。
查看数据库,数据库内信息被更改
4、删
编写映射文件CustomerMapper.xml
<!--删除操作-->
<delete id="deletebyid">
delete from customers where id=#{id}
</delete>
编写测试类deletebyid()
//删除
@Test
public void deletebyid() throws Exception {
String resource="mybatis-config.xml";
InputStream inputStream=Resources.getResourceAsStream(resource);
SqlSessionFactory sf=new SqlSessionFactoryBuilder().build(inputStream);
SqlSession s= sf.openSession();
Customer customer=s.selectOne("CustomerMapper.findbyid",1);
s.delete("CustomerMapper.deletebyid",customer);
s.commit();
s.close();
}
删除操作思路:先查询到需要删除的个体,再删除。
查看数据库,数据库信息被删除。
5、查看所有数据
编写映射文件CustomerMapper.xml
<!--查询全部-->
<select id="findall" resultType="com.ssm.mybatis.entity.Customer">
select * from customers
</select>
实体类添加toString()方法
@Override
public String toString() {
return "Customer{" +
"id=" + id +
", name='" + name + '\'' +
", age=" + age +
'}';
}
编写测试类findall()
//查询全部
@Test
public void findall() throws Exception {
String resource="mybatis-config.xml";
InputStream inputStream=Resources.getResourceAsStream(resource);
SqlSessionFactory sf=new SqlSessionFactoryBuilder().build(inputStream);
SqlSession s=sf.openSession();
List<Customer> customerList=s.selectList("CustomerMapper.findall");
for(Customer c:customerList){
System.out.println(c.toString());
}
s.commit();
s.close();
}
查看控制台,输出数据库中所有信息