Mybatis+mysql实现增删改查

目录结构

在这里插入图片描述

导入jar

  1. mybatis 依赖包
  2. mybatis包
  3. mysql包

Person.java

package com.baidu.demo1;

public class Person {
	private int id;
	private String username;
	private String password;
	private int sex;
	private String address;
	private String hobby;
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public String getUsername() {
		return username;
	}
	public void setUsername(String username) {
		this.username = username;
	}
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}
	public int getSex() {
		return sex;
	}
	public void setSex(int sex) {
		this.sex = sex;
	}
	public String getAddress() {
		return address;
	}
	public void setAddress(String address) {
		this.address = address;
	}
	public String getHobby() {
		return hobby;
	}
	public void setHobby(String hobby) {
		this.hobby = hobby;
	}
	public Person() {
		super();
		// TODO Auto-generated constructor stub
	}
	public Person(int id, String username, String password, int sex, String address, String hobby) {
		super();
		this.id = id;
		this.username = username;
		this.password = password;
		this.sex = sex;
		this.address = address;
		this.hobby = hobby;
	}
	public Person(String username, String password, int sex, String address, String hobby) {
		super();
		this.username = username;
		this.password = password;
		this.sex = sex;
		this.address = address;
		this.hobby = hobby;
	}
	
	
}

PersonDao.java

package com.baidu.demo1;

public interface PersonDao  {
	public void insert(Person person);
	public Person select(int id);
	public void update(Person person);
	public void delete(int id);
}

PersonDaoImpl.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="com.baidu.demo1.PersonDao">

<insert id="insert" parameterType="com.baidu.demo1.Person" >
    insert into messages(id,username,password,sex,address,hobby) values(#{id},#{username},#{password},#{sex},#{address},#{hobby})
</insert>

<select id="select" parameterType="int" resultType="com.baidu.demo1.Person" >
	select * from messages where id=#{id}
</select>

<update id="update" parameterType="com.baidu.demo1.Person">
update messages set username=#{username},password=#{password},sex=#{sex},address=#{address},hobby=#{hobby} where id=#{id}
</update>

<delete id="delete" parameterType="int">
	delete from messages where id=#{id}
</delete>

</mapper>

test测试类

package com.baidu.demo1;

import java.io.IOException;
import java.io.InputStream;
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.jupiter.api.Test;

import com.mysql.cj.protocol.ServerSession;

public class test {
	@Test
	public void test() {
		SqlSession sqlSession = null;
		try {
			InputStream is = Resources.getResourceAsStream("mybatis-config.xml");
			SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);
			sqlSession = sqlSessionFactory.openSession();
			PersonDao personDao = sqlSession.getMapper(PersonDao.class);
			Person p=new Person("小王", "passwd666", 1, "长春", "玩");
			personDao.insert(p);
			sqlSession.commit();
		} catch (IOException e) {
			e.printStackTrace();
			sqlSession.rollback();
		} finally {
			sqlSession.close();
		}

	}
	
	@Test
	public void test2(){
		SqlSession sqlSession = null;
		try {
			InputStream is = Resources.getResourceAsStream("mybatis-config.xml");
			SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);
			sqlSession = sqlSessionFactory.openSession();
			PersonDao personDao = sqlSession.getMapper(PersonDao.class);
			Person p=new Person(28,"xiao8","passwd",0,"jinan","play");
			personDao.update(p);
			sqlSession.commit();
		} catch (IOException e) {
			e.printStackTrace();
			sqlSession.rollback();
		} finally {
			sqlSession.close();
		}
	}
	
	@Test
	public void test3(){
		SqlSession sqlSession = null;
		try {
			InputStream is = Resources.getResourceAsStream("mybatis-config.xml");
			SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);
			sqlSession = sqlSessionFactory.openSession();
			PersonDao personDao = sqlSession.getMapper(PersonDao.class);
			personDao.delete(28);
			sqlSession.commit();
		} catch (IOException e) {
			e.printStackTrace();
			sqlSession.rollback();
		} finally {
			sqlSession.close();
		}
	}
	@Test
	public void test4(){
		SqlSession sqlSession = null;
		try {
			InputStream is = Resources.getResourceAsStream("mybatis-config.xml");
			SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);
			sqlSession = sqlSessionFactory.openSession();
			PersonDao personDao = sqlSession.getMapper(PersonDao.class);
			
			Person p=personDao.select(1);
			System.out.println(p.getId());
			System.out.println(p.getUsername());
			System.out.println(p.getPassword());
			System.out.println(p.getSex());
			System.out.println(p.getAddress());
			System.out.println(p.getHobby());
			sqlSession.commit();
		} catch (IOException e) {
			e.printStackTrace();
			sqlSession.rollback();
		} finally {
			sqlSession.close();
		}
	}
	
}

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">
<configuration>
<environments default="development">
<environment id="development">
<transactionManager type="JDBC"></transactionManager>
<dataSource type="POOLED">
<property name="driver" value="com.mysql.cj.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost:3306/lyb?serverTimezone=UTC" />
<property name="username" value="root" />
<property name="password" value="123456" />
</dataSource>
</environment>
</environments>
<mappers >
<mapper resource="com/baidu/demo/PersonDaoImpl.xml"/>
<mapper resource="com/baidu/demo1/UserDaoImpl.xml"/>
</mappers>
</configuration>
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值