基本概述
mybatis简介:使用java编写,封装了JDBC,使用ORM思想实现结果集的封装
ORM:
O:实体对象,即我们在程序中根据表的字段名建的一个个实体类
R:我们建立的数据库表
M:从R到O的映射,通过XML文件配置
mybatis帮我们做的事:
1.从配置文件中得到sessionfactory
2.由sessionfactory产生session
3.在session中完成对数据的增删改查和事物提交等
4.用完后关闭session
5.在java对象和数据库对象之间做mapper配置文件
环境搭建
1.mybatis的配置文件
(1)jdbc.properties
driver=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/noteserver?characterEncoding=utf-8&serverTimezone=UTC
username=root
password=1234
(2)mybatis全局配置文件
<?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:context="http://www.springframework.org/schema/context"
xmlns:jdbc="http://www.springframework.org/schema/jdbc"
xmlns:jee="http://www.springframework.org/schema/jee"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.2.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.2.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.2.xsd">
<!-- 读取配置文件 -->
<util:properties id="jdbc" location="classpath:spring/jdbc.properties"></util:properties>
<!-- 配置数据库连接池 -->
<bean id="dataSouce" class="org.apache.commons.dbcp2.BasicDataSource">
<property name="driverClassName" value="#{jdbc.driver}"></property>
<property name="password" value="#{jdbc.password}"></property>
<property name="url" value="#{jdbc.url}"></property>
<property name="username" value="#{jdbc.username}"></property>
</bean>
<!-- 创建sqlsession会话,用于操作数据库 -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSouce"></property>
<property name="mapperLocations" value="classpath:mapper/*.xml"></property>
</bean>
<!-- 配置Mapper接口组件扫描 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.collage.dao"></property>
</bean>
</beans>
(3)dao层xml映射文件
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//ibatis.apache.org//DTD Mapper 3.0//EN"
"http://ibatis.apache.org/dtd/ibatis-3-mapper.dtd">
<mapper namespace="com.collage.dao.NoteBookDao">
<!--对表的操作-->
</mapper>
注意:
1.映射文件的操作标签的id属性必须是dao接口的方法名
2.映射文件的mapper标签的namespace属性指定dao接口的完整路径
3.映射文件中的查询操作一定要规定返回值类型不然会报错
1277


mybatis的相关介绍及环境搭建&spm=1001.2101.3001.11974&articleId=106059818&d=1&t=3&u=43766fde9d434794894b7f8abcadbb97)

被折叠的 条评论
为什么被折叠?



