Mybatis

Mybatis

文件结构图:

pic

安装:

使用Mybatis时,需要在pom.xml中配制:

 <!-- https://mvnrepository.com/artifact/org.mybatis/mybatis -->
    <dependency>
      <groupId>org.mybatis</groupId>
      <artifactId>mybatis</artifactId>
      <version>3.4.1</version>
    </dependency>

在pom中还可以配置resources,能够扫描配置路径下的所有以.xml,.properties,.tld文件:

<resources>
      <resource>
        <directory>src/main/resources</directory>
        <includes>
          <include>**/*.properties</include>
          <include>**/*.xml</include>
          <include>**/*.tld</include>
        </includes>
        <filtering>false</filtering>
      </resource>
      <resource>
        <directory>src/main/java</directory>
        <includes>
          <include>**/*.properties</include>
          <include>**/*.xml</include>
          <include>**/*.tld</include>
        </includes>
        <filtering>false</filtering>
      </resource>
    </resources>

在资源文件下,新建db.properties文件,配置环境:

jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/db0602
jdbc.username=root
jdbc.password=111111

env_choosen=deve

在资源文件下,新建config.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>
        
        </configuration>

注意:在这个配置文件中,所有的标签都有严格的顺序

<configuration>标签下:

配置加载环境的文件: <properties resource="db.properties"/>

环境配置:

<environments default="${env_choosen}">
        <environment id="deve">
            <transactionManager type="JDBC"></transactionManager>
            <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>
    </environments>

同时,配置mappers,自动获取xml文件

方式1:

 <mapper resource="com/lanou/mapper/StudentMapper.xml"></mapper>

方式2:自动读取mapper包下的所有xml文件

 <package name="com.lanou.mapper"/>

mapper包下新建接口文件

接口文件StudentMapper下写具体的方法

如:

public interface StudentMapper {

    //查询所有学生
    List<Student> findAllStudent();


    //如果参数有多个,使用@Param注解规定参数的别名
    void insertStudent(@Param("sname") String name);

    void insertStu(Student student);

    void delStu(@Param("sid") Integer id);

    void updateStu(Student student);
}

然后建立对应的xml文件:StudentMapper.xml,在这个xml文件中写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">
        
        <mapper namespace="com.lanou.mapper.StudentMapper">




    <!--Mybatisde 结果映射

        当实体类的属性名和数据库表的列名不一致的情况下,
        需要使用结果映射来确保数据的一致性

        -->

    <resultMap id="BaseMap" type="com.lanou.bean.Student">
        <!--column:数据库表列名
            property:实体类的属性名-->
        <id column="id" property="id"/>
        <result column="name" property="name"/>
    </resultMap>


    <!--保存一段sql片段,以供反复使用-->
    <sql id="Column_List">
        id,name
    </sql>
    <select id="findAllStudent" resultType="BaseMap">
        SELECT
         <include refid="Column_List"/>
         FROM student
    </select>


    <insert id="insertStudent">

        INSERT INTO student VALUES (null,#{sname})

    </insert>

    <insert id="insertStu" parameterType="com.lanou.bean.Student">
        INSERT INTO student VALUES (NULL ,#{name})
    </insert>

    <delete id="delStu">
        DELETE FROM student WHERE id=#{sid}
    </delete>

    <update id="updateStu">
        UPDATE student SET name=#{name} WHERE id=#{id}
    </update>


</mapper>

获取SQLSessionFactory

        //1.加载config文件
        String re = "config.xml";
        InputStream stream = Resources.getResourceAsStream(re);

        //2.创建SqlSessionFactory
        SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(stream);

获取SQLSession对象

SqlSession session = factory.openSession();

getMapper

StudentMapper mapper = session.getMapper(StudentMapper.class);
然后通过 mapper调用方法

对于像 StudentMapper 这样的映射器类(Mapper class)来说,它们的映射的语句可以不需要用 XML 来做,取而代之的是可以使用 Java 注解。比如,上面的 XML 示例可被替换如下:

@Select("SELECT * FROM student WHERE id = #{id}")
Student findById(Student student);
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值