MyBatis框架连接数据库的基本使用
- 导入jar包
- 创建并配置核心配置文件
- 创建并配置映射文件
- 保证SQL语句中的id和方法名一致
- 保证映射文件中的namespace和接口的全限定名一致
/**
* MyBatis 最基本的演示
* MyBatis使用步骤
* 导入jar包
* 创建并配置核心文件
* 创建并配置映射文件
* 映射文件中SQL语句的ID要和接口方法名一致
* 映射文件中的nameSpace要和接口的全限定名一致
* 创建测试
* 获取核心配置文件的输入流
* 获取sqlSessionFactory
* 获取sqlSession
* 获取接口的动态代理子类
* 接口的动态代理子类调用方法
*
*/
下面是代码可以帮助你更好的理解
导入jar包
mybatis的jar包以及数据库连接的jar包
创建并配置核心配置文件
<?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>
<property name="jdbc.driver" value="com.mysql.jdbc.Driver"/>
</properties> -->
<!--
<properties>:设置或引入资源文件
resource:在类路径下访问资源文件
url:在网络路径或磁盘路径下访问资源文件
-->
<properties resource="jdbc.properties"></properties>
<settings>
<!-- 将下划线映射成驼峰,user_name映射为userName -->
<setting name="mapUnderscoreToCamelCase" value="true"/>
<!-- 开启延迟加载 默认是关闭的 两个属性要同时设置 -->
<!-- 开启延迟加载 -->
<setting name="lazyLoadingEnabled" value="true"/>
<!-- 是否查询所有数据 -->
<setting name="aggressiveLazyLoading" value="false"/>
</settings>
<typeAliases>
<!--
为类型设置类型别名
type:Java 类型,若只设置type,默认的别名就是类型,且不区分大小写
就是给类设置别名,例如下面的u,就可以在 映射文件SQL语句执行返回值中写u
-->
<!-- <typeAlias type="com.atguigu.bean.User" alias="u"/> -->
<package name="bean"/>
</typeAliases>
<!--
<environments>:设置连接数据库的环境
default:设置默认使用的数据库环境
-->
<environments default="mysql">
<!--
<environment>:设置某个具体的数据库的环境
id:数据库环境的唯一标示
-->
<environment id="mysql">
<!-- type="JDBC|MANAGED" -->
<transactionManager type="JDBC" />
<!-- type="POOLED|UNPOOLED|JNDI" -->
<dataSource type="POOLED">
<property name="driver" value="${jdbc.driver}" />
<property name="url" value="${jdbc.url}" />
<property name="username" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
</dataSource>
</environment>
<environment id="oracle">
<transactionManager type="JDBC" />
<dataSource type="POOLED">
<property name="driver" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost:3306/ssm" />
<property name="username" value="root" />
<property name="password" value="123456" />
</dataSource>
</environment>
</environments>
<!-- 引入映射文件 -->
<!-- 引入映射文件可以设置多个mapper
<mapper resource="" />
<mapper resource="" />
也可以使用 <package/>标签
此种写法要求mapper接口和mapper映射文件必须在同一个包下
这样方法可以在sourceFolder中建立一个和接口包名全限定名一致的包路径,以实现接口和映射文件上的物理隔离
<package name="com.atguigu.mapper"/>
这两个方法不可以混用
-->
<mappers>
<!-- <mapper resource="userMapper.xml" /> -->
<package name="mapperInterface"/>
</mappers>
</configuration>
创建并配置映射文件
<?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="mapperInterface.SqlMapper">
<!-- User selectUser1(Integer id); -->
<select id="selectUser1" resultType="User">
select id,userName,age,sex
from testuser where
id=#{id}
</select>
<!-- User selectUser2(Integer id); -->
<select id="selectUser2" resultType="User">
select id,userName,age,sex
from testuser where
id=${value}
</select>
<!-- User selectUserByMore1(Integer id,String userName); -->
<select id="selectUserByMore1" resultType="User">
select
id,userName,age,sex from testuser where id=#{0} and
userName=#{1}
</select>
<!-- User selectUserByMore2(Integer id,String userName); -->
<select id="selectUserByMore2" resultType="User">
select
id,userName,age,sex from testuser where id=${param1} and
userName='${param2}'
</select>
<!-- User selectUserByMap(Map<String,Object> map); -->
<select id="selectUserByMap" resultType="User">
select
id,userName,age,sex from testuser where id=${id} and
userName='${userName}'
</select>
<!-- User selectUserByPram(@Param("id")Integer id,@Param("UserName")String
userName); -->
<select id="selectUserByPram" resultType="User">
select
id,userName,age,sex from testuser where id=${id} and
userName='${userName}'
</select>
<!-- User selectUserByList(List list); -->
<select id="selectUserByList" resultType="User">
</select>
<!-- User selectUserByArray(Integer[] id); -->
<select id="selectUserByArray">
</select>
<!-- 自定义映射 -->
<!-- 自定义映射 <id column="id" property="id" />
<id>:设置主键的映射关系,column设置字段名,property设置属性名
<result column="sex" property="sex" />
<result>:设置非主键的映射关系,column设置字段名,property设置属性名
<select>语句中的id还是需要和接口中的方法名一致 resultMap需要和<resultMap>中的id保持一致
<resultMap>中的type是查询返回结果的类型
column是指查询到的字段名 property是指查询到的字段名映射给JavaBean中的哪个属性
-->
<resultMap type="User" id="UserDepList">
<id column="id" property="id" />
<result column="userName" property="userName" />
<result column="age" property="age" />
<result column="sex" property="sex" />
<!-- <result column="depId" property="dep.depId" />
<result column="depName" property="dep.depName" /> -->
<!-- 自定义映射中可以使用<association>来定义JavaBean中的JavaBean -->
<association property="dep" javaType="Dep">
<id column="depId" property="depId" />
<result column="depName" property="depName" />
</association>
</resultMap>
<!-- ArrayList<User> selectUserDep(); -->
<select id="selectUserDep" resultMap="UserDepList">
select
id,userName,age,sex,testdep.depId,depName from testUser left join
testdep on
testuser.id=testdep.depId
</select>
<resultMap type="User" id="moreStep">
<id column="id" property="id"/>
<result column="userName" property="userName"/>
<result column="age" property="age"/>
<result column="sex" property="sex"/>
<!--
select:分步查询的SQL的id,即接口的全限定名.方法名或namespace.SQL的id
column:分步查询的条件,注意:此条件必须是从数据库查询过得
-->
<association property="dep" select="selectDepByDepId" column="depId"></association>
</resultMap>
<!-- User selectUserDepByMoreStep(Integer id); -->
<select id="selectUserDepByMoreStep" resultMap="moreStep">
select id,userName,age,sex,depId from testuser where id=#{id}
</select>
<!-- Dep selectDepByDepId(Integer depId); -->
<select id="selectDepByDepId" resultType="Dep">
select depId,depName from testdep where depId=#{depId}
</select>
<resultMap type="Dep" id="DepUserList">
<id column="depId" property="depId"/>
<result column="depName" property="depName"/>
<collection property="list" ofType="User">
<id column="id" property="id"/>
<result column="userName" property="userName"/>
<result column="age" property="age"/>
<result column="sex" property="sex"/>
</collection>
</resultMap>
<!-- Dep selectDepUser(Integer depId); -->
<select id="selectDepUser" resultMap="DepUserList">
select testdep.depId,depName,id,userName,age,sex from
testdep left join
testuser on testdep.depId=testuser.depId
where
testdep.depId=#{depId}
</select>
<resultMap type="Dep" id="DepUserByMoreStep">
<id column="depId" property="depId"/>
<result column="depName" property="depName"/>
<!-- collection:处理一对多和多对多的关系 -->
<!-- fetchType 是否开启延迟加载 lazy开启 eager不开启 -->
<collection property="list" select="selectUserBydepId" column="depId" fetchType="eager"></collection>
</resultMap>
<!-- Dep selectDepUserByMoreStep(Integer id); -->
<select id="selectDepUserByMoreStep" resultMap="DepUserByMoreStep">
select depId,depName from testdep where depId=#{depId}
</select>
<!-- ArrayList<User> selectUserBydepId(Integer id); -->
<select id="selectUserBydepId" resultType="User">
select id,userName,age,sex from testuser where depId=#{depId}
</select>
</mapper>