此文已独家授权给【新华前后端开发】使用。其他平台使用联系作者后再使用
[TOC]
在数据库方面我们最常用的应该JDBC、Hibernate和Mybatis。通过JDBC方式连接数据库,我们会发现工作量是相当的复杂。我们得处理一些琐碎的关闭。然后入参出参我们都得自己管理。基于次产生了ORM(Object Relational Mapping)模型。
<dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.5.0</version> </dependency>
package com.github.zxhtom.mapper;
import com.github.zxhtom.model.Student;
import java.util.List;
public interface StudentMapper {
/**
* 获取学生列表
* @return
*/
public List<Student> getStudents();
/**
* 通过id获取学生信息
* @param id
* @return
*/
public Student getStudentByStuId(String id);
}
<?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.github.zxhtom.mapper.StudentMapper">
<select id="getStudents" resultType="com.github.zxhtom.model.Student">
select * from student
</select>
<select id="getStudentByStuId" resultType="com.github.zxhtom.model.Student">
select * from student where id=#{id}
</select>
</mapper>
<?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="config.properties"></properties>
<!--定义别名-->
<typeAliases>
<package name=""/>
</typeAliases>
<!--定义数据库信息,默认使用development数据库构建环境-->
<environments default="development">
<environment id="development">
<!--jdbc事物管理-->
<transactionManager type="JDBC"></transactionManager>
<!--配置数据库连接信息-->
<dataSource type="POOLED">
<property name="driver" value="${database.driver}"/>
<property name="url" value="${database.url}"/>
<property name="username" value="${database.username}"/>
<property name="password" value="${database.password}"/>
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource="com/github/zxhtom/mapper/StudentMapper.xml"></mapper>
</mappers>
</configuration>
//获取mybatis-config.xml位置
InputStream inputStream = Resources.getResourceAsStream(Constant.MYBATIS);
//加载mybatis-config,并创建sqlsessionfactory对象
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
//创建sqlsession对象
SqlSession sqlSession = sqlSessionFactory.openSession();
Map<String, Object> paramMap = new HashMap<>();
paramMap.put("id", 1);
//执行select语句,将resultSet映射成对象并返回
StudentMapper studentMapper = sqlSession.getMapper(StudentMapper.class);
List<Student> students = studentMapper.getStudents();
studentPrint(students);
Map<Object, Object> properties = PropertiesUtil.getProperties("config.properties");
PooledDataSource dataSource = new PooledDataSource();
dataSource.setDriver(properties.get("database.driver").toString());
dataSource.setUrl(properties.get("database.url").toString());
dataSource.setUsername(properties.get("database.username").toString());
dataSource.setPassword(properties.get("database.password").toString());
//构建数据库事物方式
TransactionFactory transactionFactory = new JdbcTransactionFactory();
//创建数据库运行环境
Environment environment = new Environment("development",transactionFactory,dataSource);
//构建Configure对象
Configuration configuration = new Configuration(environment);
//注册别名
configuration.getTypeAliasRegistry().registerAlias("stu", Student.class);
//加入一个映射器
configuration.addMapper(StudentMapper.class);
//使用sqlsessionfactoryBuilder构建sqlsessionfactory
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(configuration);
SqlSession sqlSession = sqlSessionFactory.openSession();
StudentMapper studentMapper = sqlSession.getMapper(StudentMapper.class);
List<Student> students = studentMapper.getStudents();
studentPrint(students);
resultMap