mybatis简单例子helloworld

一、创建数据库

二、项目目录结构

三、创建Employee类来封装数据表里的数据

package com.smc.mybatis.bean;

public class Employee {
	
	private Integer id;
	private String lastName;
	private String email;
	private String gender;
	
	
	public String getEmail() {
		return email;
	}
	public void setEmail(String email) {
		this.email = email;
	}
	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 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 + "]";
	}
	
	
	
	
}

四、创建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/mybatis"/>
 <property name="username" value="root"/>
 <property name="password" value="123456"/>
 </dataSource>
 </environment>
 </environments>
 <mappers>
 <mapper resource="EmployeeMapper.xml"/>
 </mappers>
</configuration>

五、创建测试类MyBatisTest

package com.smc.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.jupiter.api.Test;

import com.smc.mybatis.bean.Employee;

public class MyBatisTest {

	@Test
	public void test() throws IOException {
		String resource = "mybatis-config.xml";
		InputStream inputStream = Resources.getResourceAsStream(resource);
		SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
		
		SqlSession openSession = sqlSessionFactory.openSession();
		
		try {
		Employee employee = openSession.selectOne("com.smc.mybatis.EmployeeMapper.selectEmp", 1);
		System.out.println(employee);
		
		}finally {
		openSession.close();
		}
	}

}

六、创建EmployeeMapper.xml配置文件进行编写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">
<mapper namespace="com.smc.mybatis.EmployeeMapper">
	<select id="selectEmp" resultType="com.smc.mybatis.bean.Employee">
		select * from tb1_employee where id = #{id}
	</select>
</mapper>

 

七、运行发现报错

经查看是数据库的密码输错了,改正之后运行:

DEBUG 11-28 08:47:43,104 ==>  Preparing: select * from tb1_employee where id = ?   (BaseJdbcLogger.java:145) 
DEBUG 11-28 08:47:43,144 ==> Parameters: 1(Integer)  (BaseJdbcLogger.java:145) 
DEBUG 11-28 08:47:43,185 <==      Total: 1  (BaseJdbcLogger.java:145) 
Employee [id=1, lastName=null, email=tom@smc.com, gender=0]

发现lastName为null,原来是数据库里面写的是last_name,故为null。要想查到last_name的值,可以在EmployeeMapper.xml文件中书写别名的sql语句,即:

select id,last_name lastName,email,gender from tb1_employee where id = #{id}

保存运行:

DEBUG 11-28 08:50:24,873 ==>  Preparing: select id,last_name lastName,email,gender from tb1_employee where id = ?   (BaseJdbcLogger.java:145) 
DEBUG 11-28 08:50:24,916 ==> Parameters: 1(Integer)  (BaseJdbcLogger.java:145) 
DEBUG 11-28 08:50:24,966 <==      Total: 1  (BaseJdbcLogger.java:145) 
Employee [id=1, lastName=tom, email=tom@smc.com, gender=0]

 

八、优化:接口式

以下是对上述项目的优化,使用接口式编程。优点是解耦,更加安全的类型检查。

1.新建一个EmployeeMapper接口

package com.smc.mybatis.dao;

import com.smc.mybatis.bean.Employee;

public interface EmployeeMapper {
	
	public Employee getEmpById(Integer id);
}

2.修改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.smc.mybatis.dao.EmployeeMapper">
	<select id="getEmpById" resultType="com.smc.mybatis.bean.Employee">
		select id,last_name lastName,email,gender from tb1_employee where id = #{id}
	</select>
</mapper>

3.加入测试

@Test
	public void test01() throws IOException {
		// 1.获取sqlSessionFactory对象
		SqlSessionFactory sqlSessionFactory = getSqlSessionFactory();

		// 2.获取sqlSession对象
		SqlSession openSession = sqlSessionFactory.openSession();

		try {
			// 3.获取接口的实现类对象
			//会为接口自动创建一个代理对象,代理对象去执行增删改查方法
			EmployeeMapper mapper = openSession.getMapper(EmployeeMapper.class);
			Employee employee = mapper.getEmpById(1);
			System.out.println(mapper.getClass());
			System.out.println(employee);
		} finally {
			openSession.close();
		}

	}

4.运行

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值