2021-01-27

MyBatis_helloworld

一、环境搭建

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/mybait"/>
				 <property name="username" value="root"/>
				 <property name="password" value="root"/>
			 </dataSource>
		 </environment>
	 </environments>
	 <mappers>
	 	<mapper resource="org/mybatis/example/BlogMapper.xml"/>
	 </mappers>
</configuration>

 

二、新建数据库并创建Employee.java实体类

CREATE TABLE tbl_employees(
id INT(11) PRIMARY KEY AUTO_INCREMENT,
last_name VARCHAR(255),
gender CHAR(1),
email VARCHAR(255)
)
public class Employee {
	
	private Integer id;
	private String lastName;
	private String email;
	private String gender;
	public Integer getId() {
		return id;
	}
	public void setId(Integer id) {
		this.id = id;
	}
	public String getLastName() {
		return lastName;
	}
	public void setLastName(String lastName) {
		this.lastName = lastName;
	}
	public String getEmail() {
		return email;
	}
	public void setEmail(String email) {
		this.email = email;
	}
	public String getGender() {
		return gender;
	}
	public void setGender(String gender) {
		this.gender = gender;
	}
	
	@Override
	public String toString() {
		return "Employee [id=" + id + ", lastName=" + lastName + ", email=" + email + ", gender=" + gender + "]";
	}
	
	
}

三、步骤

1、根据xml配置文件(全局配置文件)创建一个SQLSessionFactory对象

有数据源一些运行环境信息

2、sql映射文件;配置了每一个SQL,以及sql的封装规则等。

3、将SQL映射文件注册在全局配置文件中

4、写代码: 

1)根据全局配置文件得到SQLSessionFactory;

 2)使用SQLSession工厂,获取到SQLSession对象使用他来执行增删改查

 一个SQLSession就代表和数据库的一次会话,用完就关闭

3)使用sql唯一标识来告诉MySatis执行哪个sql。sql都是保存在sql映射文件中

附录

SqlSession.cass添加资源的步骤

EmployeeMapper.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.fenga.mybaits.EmployeeMapper">
	<!-- 
		namespace:名称空间
		id:唯一标识
		resultType:返回值类型
		#{id}:从传递过来的参数中取id值
	 -->
	 <select id="selectEmp" resultType="com.fenga.mybatis.helloworld.Employee">
		 select id,last_name lastName,email,gender from tbl_employee where id = #{id}
	 </select>
</mapper>

测试

@Test
	public void test() throws IOException  {
		String resource = "mybatis-config.xml";
		InputStream inputStream = Resources.getResourceAsStream(resource);
		SqlSessionFactory sqlSessionFactory =
		 new SqlSessionFactoryBuilder().build(inputStream);
		
		//2、获取SqlSession实例对象,能直接执行已经映射的SQL语句
		//sql的唯一标识:statement Unique identifier matching the statement to use.parameter A 
		//执行sql使用的参数:parameter object to pass to the statement.
		SqlSession openSession = sqlSessionFactory.openSession();
		try {
			Employee employee = openSession.selectOne("com.fenga.mybaits.EmployeeMapper.selectEmp", 1);
			System.out.println(employee);
		} finally {
			openSession.close();
		}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值