Mybatis 02 对数据的CRUD(增删改查) 操作
程序员文章站
2022-05-08 08:37:57
...
1.导包
2.配置文件
<?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">
<configuration>
<environments default="development">
<environment id="development">
<transactionManager type="JDBC" />
<dataSource type="POOLED">
<property name="driver" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost:3308/mybatis" />
<property name="username" value="root" />
<property name="password" value="210374520" />
</dataSource>
</environment>
</environments>
</configuration>
3.创建实体类
package com.dhl.beyond.shiyan1;
public class User {
private int id; //用户的Id 或者是 学号
private String name; //姓名
private int age; //年龄
public User() {
}
public User(int id, String name, int age) {
this.id = id;
this.name = name;
this.age = age;
}
@Override
public String toString() {
return "User{" +
"id=" + id +
", name='" + name + '\'' +
", age=" + 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;
}
}
4.创建Mapper文件
<?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="com.dhl.beyond.shiyan3.userMapper">
<select id="selectUser" parameterType="int" resultType="com.dhl.beyond.shiyan1.User">
select * from users where id=#{id}
</select>
<insert id="insertUser" parameterType="com.dhl.beyond.shiyan1.User">
insert into users(name,age) values(#{name},#{age})
</insert>
<update id="updateUser" parameterType="com.dhl.beyond.shiyan1.User">
update users set name =#{name},age=#{age} where id = #{id}
</update>
<delete id="deleteUser" parameterType="int">
delete from users where id=#{id}
</delete>
</mapper>
5.注册
<mapper resource="com/dhl/beyond/shiyan3/userMapper.xml"/>
6.测试类
package com.dhl.beyond.shiyan3;
import com.dhl.beyond.shiyan1.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.IOException;
import java.io.Reader;
public class TestUser {
@Test
public void testSelectUser() throws IOException {
String resource = "config.xml";
Reader reader = Resources.getResourceAsReader(resource);
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
SqlSession session = sqlSessionFactory.openSession();
String s = "com.dhl.beyond.shiyan3.userMapper.selectUser";
User o = session.selectOne(s, 2);
System.out.println(o);
}
@Test
public void testInsertUser() throws IOException {
String resouce = "config.xml";
Reader reader = Resources.getResourceAsReader(resouce);
SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(reader);
String s = "com.dhl.beyond.shiyan3.userMapper.insertUser";
SqlSession session = factory.openSession();
User u = new User();
u.setAge(20);
u.setName("zhangsan");
int insert = session.insert(s, u);
System.out.println(insert);
}
@Test
public void testUpdateUser() throws IOException {
String resource = "config.xml";
Reader reader = Resources.getResourceAsReader(resource);
SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(reader);
SqlSession session = factory.openSession();
String s = "com.dhl.beyond.shiyan3.userMapper.updateUser";
int update = session.update(s, new User(2, "lisi", 22));
System.out.println(update);
}
@Test
public void testDeleteUser() throws IOException{
String resource = "config.xml";
Reader reader = Resources.getResourceAsReader(resource);
SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(reader);
SqlSession session = factory.openSession();
int delete = session.delete("com.dhl.beyond.shiyan3.userMapper.deleteUser", 3);
System.out.println(delete);
}
}
7.结果集
☯
☯☯
☯☯☯
☯☯☯☯
目录结构
推荐阅读
-
lucene全文检索实例二(实现对10万条数据检索文件的增删改查操作)
-
lucene全文检索实例二(实现对10万条数据检索文件的增删改查操作)
-
自写的利用PDO对mysql数据库增删改查操作类
-
MyBatis对MySQL数据库的增删改查操作代码详解
-
ES 16 - 对Elasticsearch中的索引数据进行增删改查 (CRUD)
-
Hibernate初学者---多对多双向关联数据表的CRUD增删改查
-
Mybatis 02 对数据的CRUD(增删改查) 操作
-
Java语言mysql数据库的访问步骤,一个简单的实例——使用DAO(数据库操作类 Data Access Object ) 实现对mysql数据库的增删改查
-
php对xml进行简单的增删改查(CRUD)操作
-
自写的利用PDO对mysql数据库增删改查操作类