Mybatis初级

创建项目:

在这里插入图片描述

User:

package com.sxt.pojo;

public class User {
	
	private Integer id;
	
	private String username;
	
	private String password;
	
	private Integer age;
	
	private Integer address;

	public User() {
		super();
	}

	public User(Integer id, String username, String password, Integer age, Integer address) {
		super();
		this.id = id;
		this.username = username;
		this.password = password;
		this.age = age;
		this.address = 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 String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public Integer getAge() {
		return age;
	}

	public void setAge(Integer age) {
		this.age = age;
	}

	public Integer getAddress() {
		return address;
	}

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

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

UserMapper.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.sxt.pojo.User">
	<select id="query" resultType="com.sxt.pojo.User">
		select * from t_user
	</select>
	<!-- 添加 -->
	<insert id="insert" parameterType="com.sxt.pojo.User">
		insert into t_user(username,password,age,address)
		values(#{username},#{password},#{age},#{address})
		
	</insert>
	<!-- 修改 -->
	<update id="update" parameterType="com.sxt.pojo.User">
		update t_user set username=#{username},password=#{password}
		,age=#{age},address=#{address} where id=#{id}
	</update>
	<!-- 删除 -->
	<delete id="delete">
		delete from t_user where id=#{id}
	</delete>
</mapper>

TestMybatis.java

package com.sxt.test;

import java.io.IOException;
import java.io.InputStream;
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.sxt.pojo.User;

public class TestMybatis {
	
	/**
	 * 查询 
	 * @throws IOException
	 */
	@Test
	public void query()throws IOException{
		//加载配置文件
		InputStream in=Resources.getResourceAsStream("mybatis-config.xml");
		
//		获取SqlSessionFactory对象
		SqlSessionFactory factory=new SqlSessionFactoryBuilder().build(in);
		//获取SqlSessionFactory对象
		SqlSession session=factory.openSession();
		//namespace+id定位执行的SQL语句
		List<User> list = session.selectList("com.sxt.pojo.User.query");
		
		for (User user : list) {
			System.out.println(user);
		}
		session.close();
	}
	
	/**
	 * 添加
	 * @throws IOException
	 */
	@Test
	public void insert() throws IOException{
		InputStream in=Resources.getResourceAsStream("mybatis-config.xml");
		SqlSessionFactory factory=new SqlSessionFactoryBuilder().build(in);
		SqlSession session=factory.openSession();
		User user=new User();
		user.setUsername("LLL");
		user.setPassword("YYY");
		user.setAge(5545);
		user.setAddress(321);
		int count=session.insert("com.sxt.pojo.User.insert",user);
		System.out.println(count);
		session.commit();
		session.close();
	}
	/**
	 * 删除
	 */
	@Test
	public void delete()throws IOException{
		
		InputStream in=Resources.getResourceAsStream("mybatis-config.xml");
		
//		获取SqlSessionFactory对象
		SqlSessionFactory factory=new SqlSessionFactoryBuilder().build(in);
		
		SqlSession session=factory.openSession();
		
		int update=session.insert("com.sxt.pojo.User.delete",18);
		
		System.out.println(update);
		
		session.commit();
		
		session.close();
		
	}
	/**
	 * 修改
	 */
	@Test
	public void update()throws IOException{
		InputStream in=Resources.getResourceAsStream("mybatis-config.xml");
		SqlSessionFactory factory=new SqlSessionFactoryBuilder().build(in);
		SqlSession session=factory.openSession();
		User user=new User();
		user.setId(12);
		user.setUsername("Y");
		user.setPassword("z");
		user.setAge(22);
		user.setAddress(21);
		int update = session.update("com.sxt.pojo.User.update", user);
		System.out.println(update);
		session.commit();
		session.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" />
			<dataSource type="POOLED">
				<property name="driver" value="com.mysql.jdbc.Driver" />
				<property name="url" value="jdbc:mysql://localhost:3306/easyui?characterEncoding=utf-8" />
				<property name="username" value="root" />
				<property name="password" value="yang" />
			</dataSource>
		</environment>
	</environments>
	<mappers>
		<mapper resource="com/sxt/pojo/UserMapper.xml"/>
		<!-- <mapper resource="com/sxt/bean/UserMapper.xml"/> -->
		<!--  <mappers>
    <mapper resource="org/mybatis/example/BlogMapper.xml"/>
  </mappers> -->
	</mappers>
</configuration>

pom.xml依赖

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.sxt</groupId>
  <artifactId>mybatis</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <dependencies>
	<dependency>
		<groupId>org.mybatis</groupId>
		<artifactId>mybatis</artifactId>
		<version>3.4.6</version>
	</dependency>
	<dependency>
		<groupId>mysql</groupId>
		<artifactId>mysql-connector-java</artifactId>
		<version>5.1.27</version>
	</dependency>
</dependencies>
  
</project>

查询测试:
在这里插入图片描述

添加测试:
添加前:
在这里插入图片描述
添加后:
在这里插入图片描述
完成

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值