Myabtis的CRUD操作

Mybatis的配置文件:

<?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>
	<!-- 和spring整合后,environments配置将废除 -->
	<environments default="development">
		<environment id="development">
			<!-- 使用jdbc事务管理,事务控制由mybatis -->
			<transactionManager type="JDBC"/>
			<!-- 数据库连接池,由mybatis控制 -->
			<dataSource type="POOLED">
				<property name="driver" value="com.mysql.jdbc.Driver"/>
				<property name="url" value="jdbc:mysql://localhost:3306/数据库名称"/>
				<property name="username" value="用户名"/>
				<property name="password" value="密码"/>
			</dataSource>
		</environment>
	</environments>
	
	<mappers>
		<mapper resource="mapper/User.xml"/>
	</mappers>
</configuration>
pojo类

package com.itachi.mybatis.bean;

import java.util.Date;

public class User {

	private int id;

	private String username;

	private String sex;

	private Date birthday;

	private String address;

	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 getSex() {
		return sex;
	}

	public void setSex(String sex) {
		this.sex = sex;
	}

	public Date getBirthday() {
		return birthday;
	}

	public void setBirthday(Date birthday) {
		this.birthday = birthday;
	}

	public String getAddress() {
		return address;
	}

	public void setAddress(String address) {
		this.address = address;
	}

	public User() {
		super();
		// TODO Auto-generated constructor stub
	}

	public User(int id, String username, String sex, Date birthday,
			String address) {
		super();
		this.id = id;
		this.username = username;
		this.sex = sex;
		this.birthday = birthday;
		this.address = address;
	}

	@Override
	public String toString() {
		return "User [id=" + id + ", username=" + username + ", sex=" + sex
				+ ", birthday=" + birthday + ", address=" + address + "]";
	}

}
映射文件:

<?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="test">
	<!-- 在映射文件中配置很多sql语句 -->
	<!-- 需求:通过id查询用户表的记录 -->
	<!-- 通过select执行数据库的查询
		id:标识映射文件中的sql,将sql语句封装到mappedStatement对象中,所以将id成为statement的id
		parameterType:指定输入参数的类型,这里指定为int
		#{}:表示一个占位符
		#{id}:其中的id表示将接收的输入参数,参数名称就是id,如果输入参数是简单类型,#{}中的参数名可以随意
		resultType:指定sql输出结果的java对象类型,select指定resultType表示将单条记录映射成java对象
	 -->
	<select id="findUserById" parameterType="int" resultType="com.itachi.mybatis.bean.User">
		SELECT * FROM USER WHERE id=#{id}
	</select>
	
	<!-- 根据用户名称模糊查询用户信息,可能返回多条记录
	${}:表示拼接sql串,将接收到的参数内容不加任何修饰拼接在sql中 
	 -->
	<select id="findUserByName" parameterType="java.lang.String" resultType="com.itachi.mybatis.bean.User">
		SELECT * FROM USER WHERE username LIKE '%${value}%'
	</select>
	
	<!-- 添加用户 -->
	<insert id="insertUser">
		INSERT INTO USER(username, birthday, sex, address) VALUES(#{username}, #{birthday}, #{sex}, #{address})
	</insert>
	
	<!-- 修改用户 -->
	<update id="updateUser"  parameterType="com.itachi.mybatis.bean.User">
		UPDATE USER SET username=#{username}, birthday=#{birthday}, sex=#{sex}, address=#{address} WHERE id=#{id}
	</update>
	
	<!-- 删除用户 -->
	<delete id="deleteUser">
		DELETE FROM USER WHERE id=#{id}
	</delete>
	
</mapper>
CRUD操作:

package com.itachi.mybatis.test;

import java.io.IOException;
import java.io.InputStream;
import java.util.Date;
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.itachi.mybatis.bean.User;

public class MybatisFirst {

	@Test 
	public void findUserByIdTest() throws IOException{
		//配置文件
		String resource  = "Mybatis-config.xml";
		//得到配置文件流
		InputStream is = Resources.getResourceAsStream(resource);
		//创建会话工厂,传入配置文件信息
		SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);
		//通过工厂得到sqlSession
		SqlSession sqlSession = sqlSessionFactory.openSession();
		//通过sqlSession操作数据库
		//第一个参数:映射文件中statement的id,等于namespace+"."+statement的id
		//第二个参数:指定和映射文件中所匹配的parameterType类型的参数
		User user = sqlSession.selectOne("test.findUserById", 1);
		System.out.println(user);
		sqlSession.close();
	}
	
	@Test
	public void findUserByNameTest() throws IOException {
		String resource = "Mybatis-config.xml";
		InputStream is = Resources.getResourceAsStream(resource);
		SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);
		SqlSession sqlSession = sqlSessionFactory.openSession();
		List<User> list = sqlSession.selectList("test.findUserByName", "小明");
		for (User user : list) {
			System.out.println(user);
		}
		sqlSession.close();
	}
	
	@Test
	public void insertUserTest() throws IOException {
		String resource = "Mybatis-config.xml";
		InputStream is = Resources.getResourceAsStream(resource);
		SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(is);
		SqlSession session = factory.openSession();
		int insert = session.insert("test.insertUser", new User(-1, "財子紫", "2", new Date(), "美國"));
		System.out.println(insert);
		session.commit();
		session.close();
	}
	
	@Test
	public void updateUserTest() throws IOException {
		String resource = "Mybatis-config.xml";
		InputStream is = Resources.getResourceAsStream(resource);
		SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(is);
		SqlSession session = factory.openSession();
		int update = session.update("test.updateUser", new User(27, "宏宇族", "2", new Date(), "新加坡"));
		System.out.println(update);
		session.commit();
		session.close();
	}
	
	@Test
	public void deleteUserTest() throws IOException {
		String resource = "Mybatis-config.xml";
		InputStream is = Resources.getResourceAsStream(resource);
		SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(is);
		SqlSession session = factory.openSession();
		int delete = session.update("test.deleteUser", 27);
		System.out.println(delete);
		session.commit();
		session.close();
	}
	
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值