[学习记录-Mybatis]Mybatis入门

环境:

Eclipse 2019
Tomcat 9.0
jdk1.8

开搞

首先创建工程

结构如下
项目结构

导入Jar包

可在mybatis官网下载 http://www.mybatis.cn/82.html
在这里插入图片描述

配置文件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>
	<typeAliases>
		<package name="com.org.entity" /> <!-- 实体类路径,方便下面配置映射文件 -->
	</typeAliases>
	<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/mybatis?characterEncoding=UTF-8" />
				<property name="username" value="root" />
				<property name="password" value="root" />
			</dataSource>
		</environment>
	</environments>
</configuration>

事务管理有两种:JDBC和MANAGED
JDBC:
在这里插入图片描述
MANAGED
在这里插入图片描述
数据源类型:UNPOOLED、POOLED、JNDI

创建实体类文件User.java

package com.org.entity;

public class User {
	private Integer id;
	private String name;
	private String password;
	public Integer getId() {
		return id;
	}
	public void setId(Integer id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}
	@Override
	public String toString() {
		return "User [id=" + id + ", name=" + name + ", password=" + password + "]";
	}
}

映射文件 User.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="user">
	<select id="findUser" resultType="User">  <!-- 查找所有用户 -->
		select * from user
	</select>
	<insert id="insertUser" parameterType="User">  <!-- 新增 -->
		insert into user
		(name,password) values (#{name},#{password})
	</insert>
	<update id="updateUser" parameterType="User"> 
		update user set name = #{name} ,password = #{password} where id = #{id}
	</update>
	
	<delete id="deleteUser" parameterType="_int">
			delete  from user where id = #{id}
	</delete>
	
</mapper>

这样一个简单的Mybatis就搭建好了,现在开始测试

Mybatis工具类

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;

public class MyBasisUtils {
	private  static SqlSessionFactory sessionFactory;
	static {
		String resources = "mybatis-config.xml";
		InputStream inputStream;
		SqlSessionFactory build = null ;
		try {
			inputStream = Resources.getResourceAsStream(resources);
			sessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	public static SqlSession openSession() {
		return sessionFactory.openSession();
	}
	
}

dao接口类

import java.util.List;
import com.org.entity.User;

public interface TestDao {
	List<User> findUser() ;
	void updateUser(User user) ;
	void insertUser(User user);
	void deleteUserById(Integer id);
}

DAO实现类 ,增删改查

import java.util.List;

import org.apache.ibatis.session.SqlSession;

import com.org.dao.TestDao;
import com.org.entity.User;
import com.org.utils.MyBasisUtils;

public class TestDaoImpl implements TestDao {
//	@Test

	@Override
	public void insertUser(User user) {
		SqlSession session = MyBasisUtils.openSession();
		session.insert("user.insertUser",user);
		session.commit();
		session.close();

	}

	@Override
	public void updateUser(User user) {
		SqlSession session = MyBasisUtils.openSession();
		session.delete("user.updateUser",user);
		session.commit();
		session.close();
	}
	@Override
	public List<User> findUser() {
		SqlSession session = MyBasisUtils.openSession();
		List<User> userList = session.selectList("user.findUser");
		session.close();
		return userList;
	}

	@Override
	public void deleteUserById(Integer id) {
		// TODO Auto-generated method stub
		SqlSession session = MyBasisUtils.openSession();
		session.delete("user.deleteUser",id);
		session.commit();
		session.close();
	}
}

后面就是简单的测试了

测试

public class Test01 {
	
	private TestDao testDao = new TestDaoImpl();
	@Test
	public void test01() {
		List<User> findUser = testDao.findUser();
		for (User user : findUser) {
			System.out.println(user);
		}
	}
	
	
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值