其他分享
首页 > 其他分享> > Mybatis 区别-开发

Mybatis 区别-开发

作者:互联网

三.MyBatis

主流的ORM 支持java .NET Ruby三种语言,MyBatis是对JDBC的封装

ORM框架Hibernate

区别:

1)sql 优化方面

2)开发方面

3)缓存机制比较

4)Hibernate 优势

5)Mybatis优势

6)应用场景

总结:

总的来说,MyBatis 是一个小巧、方便、高效、简单、直接、半自动化的持久层框架,Hibernate 是一个强大、方便、高效、复杂、间接、全自动化的持久层框架。

开发

1.配置pom.xml

   <dependencies>
<!--        mybatis-->
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis</artifactId>
            <version>3.4.5</version>
        </dependency>
<!--        mysql驱动-->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>8.0.19</version>
        </dependency>
<!--        lombok-->
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.18.10</version>
        </dependency>
    </dependencies>
<build>
    <resources>
        <resource>
            <directory>src/main/java</directory>
            <includes>
                <include>**/*.xml</include>
            </includes>
        </resource>
    </resources>
</build>

2.实体类:

package com.shouthwind.entity;

import lombok.Data;

@Data
public class People {
    private Integer Sno;
    private String Sname;
    private String Ssex;
    private Integer Sage;
    private String Sdept;
}

3.配置Mybatis的运行环境 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>
    <!-- 配置mybatis运行环境 -->
    <environments default="development">
    <environment id="development">
    <!-- 使用JDBC的事务管理 -->
    <transactionManager type="JDBC"/>
    <dataSource type="POOLED">
    <!-- MySQL数据库驱动 -->
    <property value="com.mysql.cj.jdbc.Driver" name="driver"/>
    <!-- 连接数据库的URL -->
    <property value="jdbc:mysql://localhost:3306/text?serverTimezone=UTC&amp;useUnicode=true&amp;characterEncoding=utf8" name="url"/>
    <property value="root" name="username"/>
    <property value="123456" name="password"/>
    </dataSource>
    </environment>
    </environments>
    <!-- 将mapper文件加入到配置文件中 -->
    <mappers>
    <mapper resource="com/shouthwind/Mapper/PeopleMapper.xml"/>
    </mappers>
</configuration>

4.Mabytatis开发:

二.Mapper自定义的代理接口

开发者只需要定义接口不需要实现,具体的实现由Mapper代理实现

1.自定义接口

package com.shouthwind.repository;

import com.shouthwind.entity.People;

import java.util.List;

public interface UserRepository {
    public int save(People people);
    public int deleteById(Integer Sno);
    public  int update(People people);
    public People findById(Integer Sno);
    public List<People> findAll();
}

2.创建peopleMappertaining.xml

更据规则:

1.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">
<mapper namespace="com.shouthwind.repository.UserRepository">
    <insert id="save" parameterType="com.shouthwind.entity.People">
        insert into people(name,money) values(#{name},#{money})
    </insert>

    <delete id="deleteById" parameterType="java.lang.Integer">
        delete from people where  id=#{id}
    </delete>

    <update id="update" parameterType="com.shouthwind.entity.People">
        update people set name=#{name} ,money=#{money} where id=#{id}
    </update>

    <select id="findById" parameterType="java.lang.Integer" resultType="com.shouthwind.entity.People">
        select *from people where id=#{id}
    </select>

    <select id="findAll" resultType="com.shouthwind.entity.People">
         select *from people
    </select>
</mapper>

2.接口:

package com.shouthwind.repository;

import com.shouthwind.entity.People;

import java.util.List;

public interface UserRepository {
    public int save(People people);
    public int deleteById(Integer id);
    public  int update(People people);
    public People findById(Integer id);
    public List<People> findAll();
}

3.测试类

package com.shouthwind.Test;


import com.shouthwind.entity.People;
import com.shouthwind.repository.UserRepository;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.InputStream;

public class Test2 {
    public static void main(String[] args) {
        InputStream inputStream =Test2.class.getClassLoader().getResourceAsStream("config.xml");
        SqlSessionFactoryBuilder sqlSessionFactoryBuilder= new SqlSessionFactoryBuilder();
        SqlSessionFactory sqlSessionFactory =sqlSessionFactoryBuilder.build(inputStream);
        SqlSession sqlSession=sqlSessionFactory.openSession();
        //获取实现了自定义代理接口的对象
        UserRepository userRepository=sqlSession.getMapper(UserRepository.class);
//        添加、修改、删除、查询
        People people =new People();
        people.setId(1);
        people.setName("小王");
        people.setMoney(999);
//        int row=userRepository.save(people);
        int row =userRepository.update(people);
        System.out.println(row);
//        People people=userRepository.findById(1);
//        System.out.println(people);
        //提交事物
        sqlSession.commit();
        sqlSession.close();
    }


}

标签:Hibernate,people,区别,People,MyBatis,开发,Mybatis,import,public
来源: https://www.cnblogs.com/HJZ114152/p/16406962.html