目的:使用mybatis用id查询数据

第一步:导入相对应的jar包

第二步:配置dtd约束(只有配置了约束才可以进行xml自动提示)

https://blog.csdn.net/a15920804969/article/details/79107852

第三步:准备好一张表,和相对应的pojo(domain)

3-1、user表

3-2、pojo

package com.ziyang.mybatis.pojo;

import java.io.Serializable;
import java.util.Date;

public class User implements Serializable {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private Integer id;
	private String username;
	private String sex;
	private Date birthday;
	private String 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 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;
	}
	@Override
	public String toString() {
		return "User [id=" + id + ", username=" + username + ", sex=" + sex
				+ ", birthday=" + birthday + ", address=" + address + "]";
	}

	
	

}

第四步:sqlmap的创建(也就是写sql语句的地方)

<?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">

<!-- 写sql语句的 
	namespace 命名空间,这个mapper的名字
-->
<mapper namespace="test">
	<!-- 
		通过id查询一个用户
		id:是待会我们调用那条sql的标识
		parameterType:传入的参数的类型 (支持基本数据类型简写)
		resultType:返回的数据类型
		#{xxx} : 相当于占位符 xxx随便写
	 -->
	
	<select id="findUserById" parameterType="Integer" resultType="com.ziyang.mybatis.pojo.User">
		select * from user where id = #{a}
	</select>
	
</mapper>

第五步:配置mybatis主配置文件 (sqlMapConfig.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>

	<!-- 和spring整合后 environments配置将废除    -->
	<environments default="development">
		<environment id="development">
			<!-- 使用jdbc事务管理 -->
			<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="123" />
			</dataSource>
		</environment>
	</environments>
	
	<!-- Mapper文件的位置 -->
	<mappers>
		<mapper resource="com/ziyang/mybatis/sqlmap/User.xml"/>
	</mappers>
	
</configuration>

第六步:测试:

package com.ziyang.mybatis.test;

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;
import org.junit.Test;

import com.ziyang.mybatis.pojo.User;

public class Demo_1 {
	
	@Test
	public void test1() throws IOException {
		//加载核心配置文件
		InputStream stream = Resources.getResourceAsStream("sqlMapConfig.xml");
		//创建一个sqlSessionFactory
		SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(stream);
		//创建sqlsession
		SqlSession sqlSession = sqlSessionFactory.openSession();
		//执行sql语句
		User user = sqlSession.selectOne("test.findUserById",1);
		
		System.out.println(user);
	}
}

 

 

结构目录: