mybatis的基础运用

文章目录

  • mybatis的使用步骤
  • 在这里插入图片描述
  • 数据库配置信息
  • 映射文件
  • 测试

mybatis的使用步骤

 * 	1.导入mybatis和mysql的jar包
 *  2.添加mybatis的全局配置文件(设置数据库连接信息和关联映射文件)
 *  3.创建对应的映射文件(写sql语句)和pojo类

mybatis的基础运用_第1张图片

如果Add不到的话就手动导包
mybatis的基础运用_第2张图片

数据库配置信息

<?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/szxmupms?characterEncoding=utf-8"/>
				<property name="username" value="root"/>
				<property name="password" value="1234"/>
			</dataSource>
		</environment>
	</environments>
  <mappers>
  	<!-- 关联映射文件 -->
  	<mapper resource="com/bb/pojo/UserMapper.xml"/>
  </mappers>
</configuration>

映射文件

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper
	PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
	"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<!-- namespace 名称空间:作用是用来区分各个xml映射文件的 -->
<mapper namespace="com.bb.pojo.User">
		<select id="query" resultType="com.bb.pojo.User">
			select * from t_user
		</select>
		<insert id="addUser"  parameterType="com.bb.pojo.User">
			insert into t_user(name,age)values(#{name},#{age})
		</insert>
		
		<update id="updateUser" parameterType="com.bb.pojo.User">
			update t_user set name=#{name}  ,age=#{age} where id=#{id}
		</update>
		
		<delete id="deleteById"   parameterType="int">
			delete from t_user where id =#{id}
		</delete>
		
		<select id="queryById" resultType="com.bb.pojo.User">
			select * from t_user where id = #{ids}
		</select>	
</mapper>

测试

package com.bb.test;



import java.io.IOException;
import java.io.InputStream;
import java.util.LinkedList;
import java.util.List;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.ResultContext;
import org.apache.ibatis.session.ResultHandler;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Test;

import com.bb.pojo.User;

public class MybatisTest01 {
	/**
	 * mybatis的使用步骤
	 * 	1.导入mybatis和mysql的jar包
	 *  2.添加mybatis的全局配置文件(设置数据库连接信息和关联映射文件)
	 *  3.创建对应的映射文件(写sql语句)和pojo类
	 *  @throws IOException
	 */
	@Test
	public void query() throws IOException {
		// 1.加载全局配置文件
		InputStream in = Resources.getResourceAsStream("mybatis-cfg.xml");
		// 2.获取SqlSessionFactory对象
		SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(in);
		// 3.获取SqlSession对象
		SqlSession session = factory.openSession();
		
		// 4.执行数l据库操作  namespace+"."+id
		List<User> list = session.selectList("com.bb.pojo.User.query");
		for (User user : list) {
			System.out.println(user);
		}
		// 5.关闭
		session.close();
	}
	
	@Test
	public void queryById() throws IOException {
		InputStream in = Resources.getResourceAsStream("mybatis-cfg.xml");
		SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(in);
		SqlSession session = factory.openSession();		
		List<User> list = session.selectList("com.bb.pojo.User.queryById",4);
		for (User user :list) {
			System.out.println(user);
		}
		session.close();
	}
	
	@Test
	public void addUser() throws IOException{
		 InputStream in = Resources.getResourceAsStream("mybatis-cfg.xml");
		 SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(in);
		 // 自动提交默认是关闭的
		 SqlSession session = factory.openSession();
		 User user = new User();
		 user.setName("bb");
		 user.setAge(22);		 
		 int count = session.insert("com.bb.pojo.User.addUser",user);
		 session.commit();
		 System.out.println("影响的行数"+count);
		 session.close();
	}
}

你可能感兴趣的:(mybatis的基础运用)