Mybatis框架的实现之根据id查询数据

第一步:创建数据库ceshi

第二步:创建user表并添加数据

第三步:创建一个web项目  mybaits-getOne

第三步:在src下创建包,在项目下创建源文件resources

 

第四步:导入需要的jar包,并BuilderPath

 

第五步:在com.salmon.bean下创建UserBean类

package com.salmon.bean;

public class UserBean {
 private int id;
 private String name;
 private int age;
 private String sex;
 private String address;
public int getId() {
	return id;
}
public void setId(int id) {
	this.id = id;
}
public String getName() {
	return name;
}
public void setName(String name) {
	this.name = name;
}
public int getAge() {
	return age;
}
public void setAge(int age) {
	this.age = age;
}
public String getSex() {
	return sex;
}
public void setSex(String sex) {
	this.sex = sex;
}
public String getAddress() {
	return address;
}
public void setAddress(String address) {
	this.address = address;
}
public UserBean(int id, String name, int age, String sex, String address) {
	super();
	this.id = id;
	this.name = name;
	this.age = age;
	this.sex = sex;
	this.address = address;
}

public UserBean(){
	
}
@Override
public String toString() {
	return "UserBean [id=" + id + ", name=" + name + ", age=" + age + ", sex="
			+ sex + ", address=" + address + "]";
}

 
}

第六步:在resources下创建mybatisConfig.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>
	<!-- 配置数据库连接信息 引入资源文件 -->
	<properties resource="jdbc.properties"></properties>
	
	<!-- development 开发模式 work 工作模式 -->
	<environments default="development">
		<environment id="development">
			<transactionManager type="JDBC" />
			<dataSource type="POOLED">
				<property name="driver" value="${driver}" />
				<property name="url" value="${url}" />
				<property name="username" value="${username}" />
				<property name="password" value="${password}" />

			</dataSource>
		</environment>
	</environments>
	<mappers>
		<mapper resource="com/salmon/mapper/userMapper.xml" />
	</mappers>
</configuration>

第七步:在resources下创建jdbc.properties而文件

url = jdbc:mysql://localhost:3306/ceshi?useUnicode=true&characterEncoding=utf-8
username = root
password = admin
driver=com.mysql.jdbc.Driver

第八步:在com.salmon.mapper下创建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.salmon.dao.UserDao">

  <!-- 根据id查询数据 -->
  <select id="getOne" parameterType="int" resultType="com.salmon.bean.UserBean">
      select * from user where id=#{id}
  </select>

</mapper>

第九步:在com.salmon.dao下创建UserDao接口

package com.salmon.dao;

import com.salmon.bean.UserBean;

public interface UserDao {
 
	public UserBean getOne(int id);
}

第十步:在com.salmon.util下创建SqlSessionUtil类

package com.salmon.util;

import java.io.IOException;
import java.io.Reader;

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 SqlSessionUtil {
	private static SqlSessionFactory factory;
	
	static{
		try {
			Reader	reader = Resources.getResourceAsReader("mybatisConfig.xml");
			// 第二部 获得sqlsession工厂
			factory = new SqlSessionFactoryBuilder()
					.build(reader);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public static SqlSessionFactory  getSessionFactory(){
		return factory;
	}
    
	public static SqlSession getSession(){
		return getSessionFactory().openSession();//默认不自动提交
		//return getSessionFactory().openSession(true);自动提交
	}
}

第十一步:在com.salmon.test下创建Test类

package com.salmon.test;

import org.apache.ibatis.session.SqlSession;

import com.salmon.bean.UserBean;
import com.salmon.util.SqlSessionUtil;

public class Test{
public static void main(String[] args) {
	SqlSession session = SqlSessionUtil.getSession();
	UserBean user = session.selectOne("getOne", 3);
	System.out.println(user);
	session.close();
}
}

运行结果:

 

下载地址:https://download.csdn.net/download/qq_55641896/85078800

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值