此文已独家受权给【新华先后端开发】使用。其余平台使用联系做者后再使用java
[TOC]git
在数据库方面咱们最经常使用的应该JDBC、Hibernate和Mybatis。经过JDBC方式链接数据库,咱们会发现工做量是至关的复杂。咱们得处理一些琐碎的关闭。而后入参出参咱们都得本身管理。基于次产生了ORM(Object Relational Mapping)模型。github
<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
来实现数据库字段和实体字段的映射。这就是咱们所谓的半自动映射加入战队sql