Java日常学习

Mybatis初级运用

2020-11-16  本文已影响0人  LeslieFind

官方文档:https://mybatis.org/mybatis-3/zh/getting-started.html

一、引入依赖:

        <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>
ps.pom文件中过滤xml文件,不写有时候会报找不到配置文件:
<build>
      <resources>
            <resource>
                <directory>src/main/java</directory>
                <includes>
                    <include>**/*.properties</include>
                    <include>**/*.xml</include>
                </includes>
                <filtering>false</filtering>
            </resource>
            <resource>
                <directory>src/main/resources</directory>
                <includes>
                    <include>**/*.properties</include>
                    <include>**/*.xml</include>
                </includes>
                <filtering>false</filtering>
            </resource>
        </resources>
</build>

二、编写配置文件,在classpath下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>
    <environments default="development">
        <environment id="development">
            <transactionManager type="JDBC"/>
            <dataSource type="POOLED">
                <property name="driver" value="com.mysql.jdbc.Driver"/>
                <property name="url" value="jdbc:mysql://127.0.0.1:3306/mall"/>
                <property name="username" value="root"/>
                <property name="password" value="root"/>
            </dataSource>
        </environment>
    </environments>
    <mappers>
<!--mapper接口-->
        <mapper resource="com/ld/test/demo1/mapper/pmsBrand.xml"/>
    </mappers>
</configuration>

三、mybatis的通用工具类

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.IOException;
import java.io.InputStream;

public class MybatisUtils {
    private static SqlSessionFactory factory;
    static {
        String resource = "mybatis-config.xml";
        try {
            InputStream resourceAsStream = Resources.getResourceAsStream(resource);
            factory = new SqlSessionFactoryBuilder().build(resourceAsStream);
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    public static SqlSession getSqlSession(){
        return factory.openSession();
    }
}

四、编写实体类

五、编写mapper接口

public interface PmsBrandMapper {
    List<PmsBrand> getPmsBrandAll();

    PmsBrand getPmsBrandById(int id);

    int addPmsBrand(PmsBrand pmsBrand);

    int updatePmsBrand(PmsBrand pmsBrand);
}

六、编写增删改查的配置文件,pmsBrandMapper.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.ld.test.demo1.mapper.PmsBrandMapper">
    <select id="getPmsBrandAll" resultType="com.ld.test.demo1.pojo.PmsBrand">
    select * from pms_brand;
    </select>

    <select id="getPmsBrandById" resultType="com.ld.test.demo1.pojo.PmsBrand">
        select * from pms_brand where id=#{id};
    </select>

    <select id="selectPmsBrandByIF2" parameterType="map" resultType="com.ld.test.demo1.pojo.PmsBrand">
        select * from pms_brand where id=#{id} and first_letter = #{first_letter};
    </select>

    <insert id="addPmsBrand" parameterType="com.ld.test.demo1.pojo.PmsBrand">
        insert into pms_brand (name,first_letter,sort) values (#{name},#{first_letter},#{sort});
    </insert>

</mapper>

七、测试

class Demo1ApplicationTests {

    @Test
    void testGetAll(){
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        PmsBrandMapper mapper = sqlSession.getMapper(PmsBrandMapper.class);
        List<PmsBrand> pmsBrandAll = mapper.getPmsBrandAll();

        for (PmsBrand p:
             pmsBrandAll) {
            System.out.println(p);
        }
        sqlSession.close();
    }

    @Test
    public void testAddPmsBrand(){
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        PmsBrandMapper mapper = sqlSession.getMapper(PmsBrandMapper.class);
        //long id, String name, String first_letter, int sort,
        // int factory_status, int show_status, int product_count,
        // int product_comment_count, String logo, String big_pic, String brand_story
        PmsBrand pmsBrand = new PmsBrand(88,"ld","L",700,1,1,1,1,"ldd","123","123");
        mapper.addPmsBrand(pmsBrand);
        sqlSession.commit();

        sqlSession.close();
    }
}
上一篇 下一篇

猜你喜欢

热点阅读