mybatis
,在其创建一张表CREATE TABLE employee(
id INT(11) PRIMARY KEY AUTO_INCREMENT,
last_name VARCHAR(255),
gender CHAR(1),
email VARCHAR(255)
);
-- 再插进一条随意数据,用于测试
INSERT INTO employee VALUES(1, 'tina', '女', '[email protected]');
pom.xml
<dependency>
<groupId>mysqlgroupId>
<artifactId>mysql-connector-javaartifactId>
<version>6.0.6version>
dependency>
TestJDBC
@Test
public void testJDBC() {
Connection connection = null;
Statement statement = null;
ResultSet resultSet = null;
try {
//1、注册数据库驱动
DriverManager.registerDriver(new Driver());
//2、获取数据库连接
connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/mybatis?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=CTT", "root", "123456");
//3、获取传输器对象
statement = connection.createStatement();
//4、利用传输器传输sql语句到数据库中执行,获取结果集对象
resultSet = statement.executeQuery("select * from employee");
//预处理语句:作用:可以减轻数据库的负担,提高访问数据库的速度
//statement = connection.prepareStatement("select * from user");
//5、遍历结果集获取查询结果
while (resultSet.next()) {
String id = resultSet.getString("id");
String lastName = resultSet.getString("last_name");
String gender = resultSet.getString("gender");
String email = resultSet.getString("email");
System.out.println("id: " + id);
System.out.println("userId: " + lastName);
System.out.println("userHead: " + gender);
System.out.println("createTime: " + email);
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
try {
//6、关闭资源
resultSet.close();
statement.close();
connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
MyBatis下载
MyBatis官网
pom.xml
<dependency>
<groupId>org.mybatisgroupId>
<artifactId>mybatisartifactId>
<version>3.4.1version>
dependency>
Employee.java
public class Employee {
private Integer id;
private String lastName;
private String email;
private String gender;
//getter and setter and toString()
}
mybatis全局配置文件
mybatis-config.xml
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.cj.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost:3306/mybatis?useUnicode=true&characterEncoding=UTF-8&serverTimezone=CTT" />
<property name="username" value="root" />
<property name="password" value="123456" />
dataSource>
environment>
environments>
<mappers>
<mapper resource="c01/EmployeeMapper.xml" />
mappers>
configuration>
sql映射文件
EmployeeMapper.xml
<mapper namespace="abc">
<select id="getEmpById" resultType="club.coderhome.c01.helloworld.bean.Employee">
select id,last_name lastName,email,gender from employee where id = #{id}
select>
mapper>
HelloWorldTest.java
public class HelloWorldTest {
public SqlSessionFactory getSqlSessionFactory() throws IOException {
String resource = "c01/mybatis-config.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);
return new SqlSessionFactoryBuilder().build(inputStream);
}
/**
* 1、根据xml配置文件(全局配置文件)创建一个SqlSessionFactory对象 有数据源一些运行环境信息
* 2、sql映射文件;配置了每一个sql,以及sql的封装规则等。
* 3、将sql映射文件注册在全局配置文件中
* 4、写代码:
* 1)、根据全局配置文件得到SqlSessionFactory;
* 2)、使用sqlSession工厂,获取到sqlSession对象使用他来执行增删改查
* 一个sqlSession就是代表和数据库的一次会话,用完关闭
* 3)、使用sql的唯一标志来告诉MyBatis执行哪个sql。sql都是保存在sql映射文件中的。
*
* @throws IOException
*/
@Test
public void test() throws IOException {
// 2、获取sqlSession实例,能直接执行已经映射的sql语句
// sql的唯一标识:statement Unique identifier matching the statement to use.
// 执行sql要用的参数:parameter A parameter object to pass to the statement.
SqlSessionFactory sqlSessionFactory = getSqlSessionFactory();
SqlSession openSession = sqlSessionFactory.openSession();
try {
Employee employee = openSession.selectOne(
"abc.getEmpById", 1);
System.out.println(employee);
} finally {
openSession.close();
}
}
}
日后主要用接口式编程
HelloWorld-接口式编程
EmployeeMapper.java
import club.coderhome.c01.helloworld.bean.Employee;
public interface EmployeeMapper {
public Employee getEmpById(Integer id);
}
EmployeeMapper2.xml
<mapper namespace="club.coderhome.c01.helloworld.dao.EmployeeMapper">
<select id="getEmpById" resultType="club.coderhome.c01.helloworld.bean.Employee">
select id,last_name lastName,email,gender from employee where id = #{id}
select>
mapper>
HelloWorldTest.java
@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();
}
}
EmployeeMapper empMapper = sqlSession.getMapper(EmployeeMapper.class);