mybatis 入门案例 增删改查

mybatis 入门案例 增删改查

1 导入相关jar包

  	<dependency>
  		<groupId>mysql</groupId>
  		<artifactId>mysql-connector-java</artifactId>
  		<version>5.1.6</version>
  	</dependency>
  	<dependency>
  		<groupId>junit</groupId>
  		<artifactId>junit</artifactId>
  		<version>4.12</version>
  	</dependency>
  	<dependency>
  		<groupId>org.mybatis</groupId>
  		<artifactId>mybatis</artifactId>
  		<version>3.5.6</version>
  	</dependency>

2.创建持久化类

package com.hopu.dao;

public class Account {
	private String name;
	private double money;
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public double getMoney() {
		return money;
	}
	public void setMoney(double money) {
		this.money = money;
	}
	@Override
	public String toString() {
		return "Account [name=" + name + ", money=" + money + "]";
	}
	
}

3.创建映射文件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="AccountMapper">
  	<select id="findall" resultType="com.hopu.dao.Account">
  		select * from account
  	</select>
  	<update id="update" parameterType="com.hopu.dao.Account">
  		update account set money = #{money} where name = #{name}
  	</update>
  	<update id="delete" parameterType="com.hopu.dao.Account">
  		delete from account where name= #{name}
  	</update>
  	<update id="inster" parameterType="com.hopu.dao.Account">
  		insert into account (name,money) values(#{name},#{money})
  	</update>
  </mapper>

4创建jdbc配置文件

driverClass=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/test
username=root
password=root

4.创建 MyBatis 的配置文件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>
  	<!--导入配置jdbc文件 -->
 	<properties resource="jdbc.properties"/>
 	<!-- 配置mybatis运行环境 -->
  	<environments default="development">
  	<environment id="development">
  	<!-- 使用JDBC的事务管理 -->
  		<transactionManager type="jdbc"></transactionManager>
  		<dataSource type="POOLED">
  			<!-- MySQL数据库驱动 -->
  			<property name="driver" value="${jdbc.driver}"/>
  			<!-- 连接数据库的URL -->
  			<property name="url" value="${jdbc.url}"/>
  			<property name="username" value="${jdbc.username}"/>
  			<property name="password" value="${jdbc.password}"/>
  		</dataSource>
  		</environment>
  	</environments>
  	 <!-- 将mapper文件加入到配置文件中 -->
  	<mappers>
  		<mapper resource="app.xml"></mapper>
  	</mappers>
  </configuration>

5.创建测试类

package test;

import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.util.List;
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 com.hopu.dao.Account;

public class test {
	private SqlSession Setting() throws IOException {
		InputStream a = Resources.getResourceAsStream("mybites.xml");
		SqlSessionFactory sql = new SqlSessionFactoryBuilder().build(a);
		SqlSession aa = sql.openSession();
		return aa;
	}
	@Test
	public void mybatis() throws IOException {
		SqlSession aa = Setting();
		List<Object> s = aa.selectList("AccountMapper.findall");
		System.out.println(s);
	}
	//修改
	@Test
	public void update() throws Exception {
		SqlSession aa = Setting();
	    Account updatemu = new Account();
	    updatemu.setMoney(100.0);
        updatemu.setName("张三");
        
        aa.update("AccountMapper.update", updatemu);
        // 提交事务
        aa.commit();
        // 关闭 SqlSession
        aa.close();
	}

	//删除
	@Test
	public void delete() throws Exception {
		SqlSession aa = Setting();
	    Account updatemu = new Account();
	    updatemu.setName("张三");
	    aa.delete("AccountMapper.delete", updatemu);
	    // 提交事务
        aa.commit();
        // 关闭 SqlSession
        aa.close();
	}
	//添加
	@Test
	public void inster() throws Exception {
		SqlSession aa = Setting();
	    Account updatemu = new Account();
	    String name="王五1";
	    double money=1000.0;
	    updatemu.setMoney(money);
	    updatemu.setName(name);
	    aa.insert("AccountMapper.inster", updatemu);
	    // 提交事务
        aa.commit();
        // 关闭 SqlSession
        aa.close();
	}
		
 
    }



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Vue1024

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值