mybatis实现简单的增删改查
程序员文章站
2022-07-12 22:38:11
...
1.创建项目
然后next,finally。
2.打开pom.xml引入配置文件
在原有代码的下方插入以下代码
<packaging>jar</packaging>
<dependencies>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.3</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.18</version>
</dependency>
<!-- 导入日志坐标-->
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.12</version>
</dependency>
<!--导入单元测试坐标-->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.10</version>
</dependency>
</dependencies>
3.引入log4j.propertise
创建该类型的文件,在网上找到代码粘进去
4.创建实体类以及持久层接口
简单的用户信息为例
User.java
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 + '\'' +
'}';
}
}
UserDao.java
public interface UserDao {
//查询所有用户
List<User> findAll();
//保存用户
void saveUser(User user);
//用户的更新操作
void updateUser(User user);
//根据id删除用户
void deleteUser(Integer id);
//根据id查询
User findById(Integer id);
//根据用户名称模糊查询用户信息
List<User> findByName(String username);
//查询总记录条数
Integer findTotal();
//根据queryVo中的条件查询用户
List<User> findUserByVo(QueryVo vo);
}
5.配置mybatis主配置文件SqlMapConfig.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>
<!--配置环境-->
<environments default="mysql">
<!-- 配置mysql的环境-->
<environment id="mysql">
<!-- 配置事务类型-->
<transactionManager type="JDBC"></transactionManager>
<!-- 配置数据连接池-->
<dataSource type="POOLED">
<!-- 配置连接数据库的基本信息-->
<property name="driver" value="com.mysql.cj.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/dream?serverTimezone=UTC"/>
<property name="username" value="root"/>
<property name="password" value="123456"/>
</dataSource>
</environment>
</environments>
<!-- 指定映射配置文件位置,映射配置文件指的是每个dao独立的配置文件-->
<mappers>
<mapper resource="com/bian/dao/UserDao.xml"/>
</mappers>
</configuration>
6.在resources目录下创建UserDao.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="com.bian.dao.UserDao">
<!--配置查询所有 -->
<select id="findAll" resultType="com.bian.domain.User">
select * from user;
</select>
<!--配置保存用户 -->
<insert id="saveUser" parameterType="com.bian.domain.User">
<selectKey keyProperty="id" keyColumn="id" resultType="int" order="AFTER">
select last_insert_id();
</selectKey>
insert into user(username,address,sex,birthday)values(#{username},#{address},#{sex},#{birthday});
</insert>
<update id="updateUser" parameterType="com.bian.domain.User">
update user set username=#{username},address=#{address},sex=#{sex},birthday=#{birthday} where id=#{id};
</update>
<delete id="deleteUser" parameterType="java.lang.Integer">
delete from user where id=#{uid};
</delete>
<select id="findById" parameterType="java.lang.Integer" resultType="com.bian.domain.User">
select * from user where id = #{aaa};
</select>
<select id="findByName" parameterType="string" resultType="com.bian.domain.User">
select * from user where username like #{name}
</select>
<select id="findTotal" resultType="java.lang.Integer">
select count(id) from user;
</select>
<!-- 根据queryVo的条件查询用户-->
<select id="findUserByVo" parameterType="com.bian.domain.QueryVo" resultType="com.bian.domain.User">
select * from user where username like #{user.username}
</select>
</mapper>
7.编写测试类
package com.bian.text;
import com.bian.dao.UserDao;
import com.bian.domain.QueryVo;
import com.bian.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.After;
import org.junit.Before;
import org.junit.Test;
import java.io.InputStream;
import java.util.Date;
import java.util.List;
public class MybatisText {
private InputStream in;
private SqlSession session;
private UserDao userDao;
@Before
public void init() throws Exception{
//1读取配置文件
in = Resources.getResourceAsStream("sqlMapConfig.xml");
//2.创建SqlSessionFactory工厂
SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder();
SqlSessionFactory factory = builder.build(in);
//3.使用工厂生产SqlSession对象
//构建者模式,把对象的创建过程隐藏,使用者只调用方法就可以拿到对象。降低类之间的依赖
session = factory.openSession();
//4.使用SqlSession创建Dao接口的代理对象
userDao = session.getMapper(UserDao.class);
}
@After
public void destroy() throws Exception{
//提交事务
session.commit();
//6.释放资源
session.close();
in.close();
}
@Test
public void textFindAll(){
//5.使用代理对象执行方法
List<User> users = userDao.findAll();
for(User user:users){
System.out.println(user);
}
}
@Test
public void textSave(){
User user = new User();
user.setUsername("name");
user.setAddress("address");
user.setSex("sex");
user.setBirthday(new Date());
System.out.println(user);
//5.使用代理对象执行方法
userDao.saveUser(user);
System.out.println(user);
}
@Test
public void textUpdate(){
User user = new User();
user.setId(1);
user.setUsername("name");
user.setAddress("address");
user.setSex("sex");
user.setBirthday(new Date());
//5.使用代理对象执行方法
userDao.updateUser(user);
}
@Test
public void textDelete(){
userDao.deleteUser(1);
}
@Test
public void textFindById(){
User user = userDao.findById(1);
System.out.println(user);
}
@Test
public void textFindByName(){
List<User> users = userDao.findByName("%n%");
for(User user:users){
System.out.println(user);
}
}
@Test
public void textFindTotal(){
Integer count = userDao.findTotal();
System.out.println("总用户量为"+count);
}
@Test
public void textFindByVo(){
QueryVo vo = new QueryVo();
User user = new User();
user.setUsername("%n%");
vo.setUser(user);
List<User> users = userDao.findUserByVo(vo);
for(User u:users){
System.out.println(u);
}
}
}
目录截图
下一篇: mybatis实现简单的增删改查操作
推荐阅读
-
PHP MySql增删改查的简单实例_php实例
-
Spring boot + mybatis + Vue.js + ElementUI 实现数据的增删改查实例代码(一)
-
Spring boot + mybatis + Vue.js + ElementUI 实现数据的增删改查实例代码(二)
-
C# 对MongoDB 进行增删改查的简单操作实例
-
Python+MongoDB自增键值的简单实现
-
JDBC 实现通用的增删改查基础类方法
-
MyBatis利用MyCat实现多租户的简单思路分享
-
C#基于DBContext(EF)实现通用增删改查的REST方法实例
-
Windows 8 Metro用C#连接SQLite及创建数据库,数据表的增删改查的实现
-
JDBC 实现通用的增删改查基础类方法