此文已獨家受權給【新華先後端開發】使用。其餘平臺使用聯繫做者後再使用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