Mybatis
来源:互联网 发布:乾隆母亲 知乎 编辑:程序博客网 时间:2024/05/22 13:29
Mybatis
1. Mybatis主要是让软件工程师将精力主要放在sql中,通过mybatis提供的映射方式,可以将数据封装成java Bean对象
2. 操作步骤:
1. 配置pom.xml文件,引入需要的jar包。
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
<dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.3.1</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.38</version>
</dependency>
2. 在Spring上下文中配置
首先要在mybatis—configs.xml中配置连接数据库的环境
<?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>
<environments default="xtDevelopment"><!-- default里面的属性值是自定义的 -->
<environment id="xtDevelopment">
<transactionManager type="JDBC"></transactionManager>//配置事务
<dataSource type="POOLED">//配置连接池
<property name="driver" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/eg"/>
<property name="username" value="root"/>
<property name="password" value="l19980207"/>
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource="mapper/userMapper.xml"/>/**这些xml文件是对sql语句的执行*/
<mapper resource="mapper/userMapper1.xml"/>
</mappers>
</configuration>
配置一个mapper.xml文件,将它引入到configuration中,在里面执行sql语句。
<?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">
<!-- statementId namespace + id -->
<mapper namespace="com.xt.userMapper">
<select id="queryUserInfoById" parameterType="String" resultType="com.xt.mybatis.entity.User">
select user_name userName,user_no userNo,age from user_info where user_no = #{value};
</select>
<select id="queryUserInfo" parameterType="String" resultType="com.xt.mybatis.entity.User">
select user_name userName,user_no userNo,age from user_info where user_no like '%${value}%';
</select>
<insert id="insertUserInfo" parameterType="com.xt.mybatis.entity.User">
insert into user_info values(#{userName},#{userNo},#{age});
</insert>
<update id="updateUserInfo" parameterType="com.xt.mybatis.entity.User" >
update user_info set user_name =#{userName},age = #{age} where user_no = #{userNo};
</update>
<delete id="deleteUserInfo" parameterType="String" >
delete from user_info where user_no = #{userNo};
</delete>
</mapper>
#value表示采用预处理的方式,即“?”。
${value}表示直接将值传入即可。
statementId是表示sql语句的唯一标识。parameterType表示传入值的类型,resultType表示返回值的类型,往往是一个javaBean对象表示查询出的一条数据。
在Test.java中进行测试.
SqlSessionFactory ssf = new SqlSessionFactoryBuilder.build(Resource.getResourceAsStream(“mybatis/mybatis-config.xml”));
SqlSession ss = ssf.openSession();
在@Before标签内进行初始化。
之后进行测试
@Test
public void queryUserInfoById(){
User user = ss.selectOne("com.xt.userMapper.queryUserInfoById","123456");
System.out.println("-->"+user);
}
通过SqlSession提供的方法对数据库进行操作,传入的数据类型要和statementId中的parameterType中的数据类型相同
其中的方法中的执行sql语句的statementId为namespace+Id。
2. 通过mapper代理的方式进行访问
通常要在Dao层中写完整的代码执行对数据库的操作,具体操作如上所示。这个时候,可以通过mapper代理的方式,通过接口即可操作数据库。
这个时候,需要将environments中的namespace属性值设置成接口的全类名,
<mapper namespace="com.xt.mybatis.mapper.UserMapping">
然后,将sql的statementId和接口中的方法一致。parameterType和接口中的形参一致,resultType与接口中的返回值要一致。
3. resultMap的映射形式:
我们知道,数据库的字段命名方式和bean中的命名方式不一样,这样将导致我们在查询过程中的sql语句会比较复杂冗余,我们可以通过resultMap标签将数据库的字段转化为bean中的字段。
<resultMap type="com.xt.mybatis.entity.User" id="user">
<result column="user_name" property="userName"></result>
<result column="user_no" property="userNo"></result>
</resultMap>
在引用的时候需要加上resultMap="user"这个属性。
4. mybatis的一些补充
我们知道,我们需要在mybatis-configs.xml文件里面配置environments连接数据库,当我们设置数据库的属性时,我们可以通过<properties></properties>标签将一些共有的属性设置到外部,然后通过el表达式的形式进行引用
<!--共有属性的配置-->
<properties>
<property name="driver" value="com.mysql.jdbc.Driver"></property>
<property name="url" value="jdbc:mysql://localhost:3306/eg"></property>
<property name="username" value="root"></property>
<property name="password" value="l19980207"></property>
</properties>
<!--当我们需要引用这些属性时,只需要使用el表达式即可-->
<property name="driver" value="${driver}"/>
<property name="url" value="${url}"/>
<property name="username" value="${username}"/>
<property name="password" value="${password}"/>
我们还可以引用一些外部文件。在<properties></properties>中的resource 属性设置为外置的properties文件。通过这种方法在外部配置而且可以动态替换。
<typeAliases></typeAliases>标签可以对类名进行重命名,
<typeAliases>
<typeAlias type="com.xt.mybatis.entity.User" alias="user"></typeAlias>
</typeAliases>
这样我们就可以在使用该类时使用重命名的名字进行定义了
<resultMap type="user" id="user">
在引用时就像这样就可以了。
当然如果我们想要将多个javaBean起别名的时候,就需要在<typeAliases></typeAliases>内设置<package>属性了,这个时候我们将需要的包写入,在需要使用时,需要将对应的javaBean的首字母小写进行重定义即可。
5. mapper文件中sql语句的动态组装。
在之前使用jdbc操作数据库时我们会需要对sql语句进行动态组装,让sql语句按照不同的要求进行操作。在mybatis里面我们可以使用
<where>标签的形式对里面的值进行设置
通过<if>标签对合适的sql句段进行拼装
<select id="queryUserInfoByIdAndName" parameterType="map" resultType="com.xt.mybatis.entity.User" resultMap="userMap">
select * from user_info
<where>
<if test="userName != null and userName != ''">
and user_name = #{userName}
</if>
<if test="userNo != null and userNo != ''">
and user_no = #{userNo}
</if>
</where>
</select>
当然如果我们的sql操作的语句有许多可以重复使用的部分,那么我们可以将这些字段抽离出来,写到一个<sql>标签里面
<sql id="queryUserWhere">
<where>
<if test="userName != null and userName != ''">
and user_name = #{userName}
</if>
<if test="userNo != null and userNo != ''">
and user_no = #{userNo}
</if>
</where>
</sql>
在需要引用的时候通过一个<include> 标签进行引用 ,在里面的refid标签内进行设置,将sql的id属性设置进来
<select id="queryUserInfoByIdAndName" parameterType="map" resultType="com.xt.mybatis.entity.User" resultMap="userMap">
select * from user_info
<include refid="queryUserWhere"></include>
</select>
beanUTO数据传输对象
当我们需要在一组数据中进行查询时,需要<foreach>标签
Foreach标签中的属性说明
Collection 传入的值的集合
Item 集合中每个属性的命名
Open 开始时的字符
Close 结束时的字符
Separator 分割字符
<sql id="queryUserByList">
<where>
<if test="userName != null and userName != ''">
and user_name = #{userName}
</if>
<if test="userNo != null and userNo != ''">
and user_no = #{userNo}
</if>
<if test="ids != null">
and user_id in
<foreach collection="ids" item="userId" open="(" close=")" separator=",">
#{userId}
</foreach>
</if>
</where>
</sql>
- MyBatis
- MyBatis
- Mybatis
- myBatis
- mybatis
- MyBatis
- mybatis
- Mybatis
- MyBatis
- Mybatis
- mybatis
- MyBatis
- MyBatis
- mybatis
- MyBatis
- mybatis
- mybatis
- mybatis
- [SQL Server无法连接到服务器]标题: 连接到服务器 --------- 无法连接到 ****
- 计算机网络读书笔记——网络层(2)
- 1.实现一个函数,可以左旋字符串中的k个字符。 ABCD左旋一个字符得到BCDA ABCD左旋两个字符得到CDAB
- python字符串/元组/列表/字典互转
- 加解密-overview
- Mybatis
- 圆周率
- oracle数据库面试题
- 日期类的时间从为什么是从1970年1月1日开始
- web前端面试题:实习岗位
- 初学Qt:指针类型的链表清空需要首先qDeleteAll
- 神经网络是什么?如何通俗理解神经网络
- CSS基础
- for循环中的++i,和方法语句中的i++的区别