关于java:8mybatis的基本工作流程20※

9次阅读

共计 4520 个字符,预计需要花费 12 分钟才能阅读完成。

mybatis 的根本工作流程
1. 读取配置文件,配置文件蕴含数据库连贯信息和 Mapper 映射文件或者 Mapper 包门路。

2. 有了这些信息就能创立 SqlSessionFactory,SqlSessionFactory 的生命周期是程序级, 程序运行的时候建设起来, 程序完结的时候沦亡

3.SqlSessionFactory 建设 SqlSession, 目标执行 sql 语句,SqlSession 是过程级, 一个办法中建设, 办法完结应该敞开

4. 当用户应用 mapper.xml 文件中配置的的办法时,mybatis 首先会解析 sql 动静标签为对应数据库 sql 语句的模式,并将其封装进 MapperStatement 对象,而后通过 executor 将 sql 注入数据库执行,并返回后果。

5. 将返回的后果通过映射,包装成 java 对象。

1、jdbc.properties,上面会调用

`jdbc.username=root`
`jdbc.password=root`
`jdbc.driverClass=com.mysql.jdbc.Driver`
`jdbc.url=jdbc:mysql://localhost:3306/ 数据库 `

2、mapper 配置

`<?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.....IUserDao">`

1 在 mybatis 中,映射文件中的 namespace 是用于绑定 Dao 接口的,即面向接口编程。
当你的 namespace 绑定接口后,你能够不必写接口实现类,mybatis 会通过该绑定主动帮你找到对应要执行的 SQL 语句

`public interface IUserDao extends IBaseDao<User>{`
 `public void modifyPassword(User user);`
`}`

对应 xml 有

 `<!-- 批改明码 -->`
 `<update id="modifyPassword" parameterType="com.qf.entity.User">`
 `UPDATE t_user set password=#{password} where id=#{id};`
 `</update>`

2 resultMap 的应用
在 mybatis 中有一个 resultMap 标签,它是为了映射 select 查问进去后果的汇合,其次要作用是将实体类中的字段与数据库表中的字段进行关联映射。
2.1 数据库与实体类之间名称雷同
前提要实体类和数据库字段名称一毛一样,理论个别不这样,在 mapper 中的查问形式:

2.2 数据库与实体类之间不雷同
第一种:开启驼峰规定
mybtis 中开启

`<setting name="mapUnderscoreToCamelCase" value="true" />`

<?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="menu.mysql">
<select id="selectMenu" resultType="org.me.menu.Menu">
select MENU_ID, MENU_NAME, PARENT_ID, URL, TITLE, LEAF, ORDER_SEQ
from mysql.MENU
order by ORDER_SEQ
</select>
</mapper>


第二种:应用 resultMap 标签来映射  
而后在 mapper.xml 中书写 resultMap 标签,使得数据库字段和实体类  
名称映射。(将实体类字段与数据库字段在标签中进行一一映射)![image.png](4)

<resultMap id="BaseResultMap" type="com...entity.User" >
<id column="id" property="id" jdbcType="INTEGER"/>
<result column="username" property="username" jdbcType="VARCHAR" />
<result column="password" property="password" jdbcType="VARCHAR" />
<result column="age" property="age" jdbcType="INTEGER" />
<result column="sex" property="sex" jdbcType="INTEGER" />
<result column="birthday" property="birthday" jdbcType="DATE" />
<result column="create_time" property="createTime" jdbcType="TIMESTAMP" />
<result column="create_user" property="createUser" jdbcType="INTEGER" />
<result column="update_time" property="updateTime" jdbcType="TIMESTAMP" />
<result column="update_user" property="updateUser" jdbcType="INTEGER" />
<result column="email" property="email" jdbcType="VARCHAR" />
<result column="flag" property="flag" jdbcType="INTEGER" />
<result column="png" property="png" jdbcType="LONGVARCHAR" />
</resultMap>


3、配置数据库  
什么是数据源  
JDBC2.0 提供了 javax.sql.DataSource 接口,它负责建设与数据库的连贯,当在应用程序中拜访数据库时  
不用编写连贯数据库的代码,间接援用 DataSource 获取数据库的连贯对象即可。用于获取操作数据 Connection 对象。

<!-- 1. 加载配置文件 -->
<context:property-placeholder location="classpath:jdbc.properties"/>
<!-- 2.dataSource -->
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close">
<property name="driverClassName" value="${jdbc.driverClass}"/>
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
</bean>


4、spring-mybatis.xml 外围文件的配置

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:lang="http://www.springframework.org/schema/lang"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/lang http://www.springframework.org/schema/lang/spring-lang.xsd">
<!-- 蕴含配置文件进来 -->
<import resource="classpath:spring-datasource.xml"/>
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="typeAliasesPackage" value="com....entity"/>
<property name="mapperLocations" value="classpath:mapper/*.xml"/>
<property name="plugins">
<array>
<bean class="com.github.pagehelper.PageInterceptor"></bean><!-- 这里用了 pagehelper 插件在 mybtis 的配置,就是分页的插件 -->
</array>
</property>
</bean>
<bean id="tx" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean>
<tx:annotation-driven transaction-manager="tx"/>
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.qf.dao"/>
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
</bean>
</beans>

正文完
 0