Mybatis持久层框架

一.依赖

<dependency>
   <groupId>org.mybatis</groupId>
   <artifactId>mybatis</artifactId>
   <version>3.5.2</version>
</dependency>
<dependency>
   <groupId>mysql</groupId>
   <artifactId>mysql-connector-java</artifactId>
   <version>5.1.47</version>
</dependency>

二.Mybatis核心配置文件

<?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="db.properties"/>
    <!--    起别名,别名为:类名且首字母小写(大写也不会影响程序)-->
     
    <environments default="mysql">

        <environment id="mysql">

            <transactionManager type="JDBC"></transactionManager>

            <dataSource type="POOLED">

                <property name="driver" value="${driver}"/>
                <property name="url" value="${url}"/>
                <property name="username" value="${username}"/>
                <property name="password" value="${password}"/>
            </dataSource>
        </environment>
    </environments>
    <!--映射-->
    <!--resource要求 接口和xml文件需要在同一个包下,且名称一致-->
   <mappers>
       <mapper resource="com/atyuan/dao/userMapper.xml"/>
   </mappers>
</configuration>

1.资源引用

(1)方式一:

<!-- 使用相对于类路径的资源引用 -->
<mappers>
 <mapper resource="org/mybatis/builder/PostMapper.xml"/>
</mappers>

 (2)方式二:

<!-- 使用完全限定资源定位符(URL) -->
<mappers>
 <mapper url="file:///var/mappers/AuthorMapper.xml"/>
</mappers>

 (3)方式三:

<!--
使用映射器接口实现类的完全限定类名
需要配置文件名称和接口名称一致,并且位于同一目录下
-->
<mappers>
 <mapper class="org.mybatis.builder.AuthorMapper"/>
</mappers>

(4)方式四:

<!--
将包内的映射器接口实现全部注册为映射器
但是需要配置文件名称和接口名称一致,并且位于同一目录下
-->
<mappers>
 <package name="org.mybatis.builder"/>
</mappers>

 常用方式一、方式二

2.连接数据库的配置信息db.properties

driver=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/mybatis?userSSL=true&useUnicode=true&characterEncoding=utf-8
username=root
password=root

自定义Mybatis工具类

public class MybatisUtils{

       private SqlSessionFactory sqlSessionFactory;

       static{

            try {
//            mybatis的配置文件名
               String resource = "mybatis-config.xml";
//            读取配置文件信息
               InputStream inputStream = Resources.getResourceAsStream(resource);
//            因为sqlSessionFactory不能直接new对象,所以通过SqlSessionFactoryBuilder类的方法build进行实例
               sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
            } catch (IOException e) {
                  e.printStackTrace();
            }

       }
       public static getSqlSession(){
//           通过sqlSessionFactory创建sqlSession
//            openSession有重载的方法,参数可传递true,表示自动提交,而默认是false,开启事务管理
            return sqlSessionFactory.openSession();
       }







}

Mapper.xml配置文件

<?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">
<!--namespace绑定对应的Dao/Mapper接口-->
<mapper namespace="com.atyuan.dao.UserMapper">
<!--    查询语句  id对应Dao的方法名-->
    <select id="getUserList" resultType="com.atyuan.pojo.User">
        select * from user;
    </select>
    <select id="getUserById" resultType="com.atyuan.pojo.User" parameterType="int">
        select  * from user where id = #{id}
    </select>
    <insert id="addUser" parameterType="com.atyuan.pojo.User">
        insert into user (id,username,birthday,sex,address) values(#{id},#{username},#{birthday},#{sex},#{address});
    </insert>
    <update id="updateUser" parameterType="com.atyuan.pojo.User">
        update user set username=#{username} where id = #{id}
    </update>
    <delete id="deleteUser" parameterType="com.atyuan.pojo.User">
        delete from user where id=#{id}
    </delete>
</mapper>

dao层下的mapper.xml文件配置增删改查操作

namespace:用于绑定对应的Dao/Mapper接口

id:对应接口中的方法名

resultType:表示返回值类型

parameterType:表示参数返回的类型(若mybatis的核心配置文件中没有配置typeAlias,则返回值是对象就需要写全限定类名)

Maven静态资源过滤问题

 <!--    在build中配置resources,来防止我们资源导出失败的问题-->
    <build>
        <resources>
            <resource>
                <directory>src/main/resources</directory>
                <includes>
                    <include>**/*.properties</include>
                    <include>**/*.xml</include>
                </includes>
                <filtering>false</filtering>
            </resource>
            <resource>
                <directory>src/main/java</directory>
                <includes>
                    <include>**/*.properties</include>
                    <include>**/*.xml</include>
                </includes>
                <filtering>false</filtering>
            </resource>
        </resources>
    </build>

小结:

  • 所有的增删改操作都需要提交事务!---因为sqlSessionFactory.openSession()默认开启事务,意味着setAutoCommit()被设置为false

  • 接口所有的普通参数,尽量都写上@Param参数,尤其是多个参数时,必须写上!          此时在进行增删该操作时,获取的值就是@Param中的(“属性名”)如:  

  • select* from where id = #{id}
  • 有时候根据业务的需求,可以考虑使用map传递参数!

    因为map是以键值对的形式存储数据,所以在获取值时直接#{“键名”} 即可

  • 为了规范操作,在SQL的配置文件中,我们尽量将Parameter参数和resultType都写上!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值