mybatis——IDEA第一次配置(一)

一.简介

MyBatis 是支持普通 SQL 查询,存储过程和高级映射的优秀持久层框架,其几乎消除了所有的 JDBC 代码和参数的手工设置以及结果集的检索。MyBatis 使用简单的 XML 或注解用于配置和原始映射,将接口和 Java 的 POJOs(Plain Old Java Objects,普通的 Java对象)映射成数据库中的记录。MyBatis 应用程序大都使用 SqlSessionFactory 实例,SqlSessionFactory 实例可以通过 SqlSessionFactoryBuilder 获得,而 SqlSessionFactoryBuilder 则可以从一个 XML 配置文件或者一个预定义的配置类的实例获得。
 

2 构建步骤

下载 MyBatis 框架包 https://github.com/mybatis/mybatis-3/releases 我下载了mybatis-3.4.6
导入 MyBatis 框架的 jar 包;
创建核心配置文件 config.xml ,作用于和数据库连接等配置。
创建映射文件 personMapper.xml,对应的数据库的实体类。
创建测试类。
其中,MyBatis 框架的 jar 包可以通过“MyBatis 之 各种依赖包 ”进行下载,而且里面包含了大多数常用的配置文件,值得大家get。此外,还有一点需要大家注意,那就是 MyBatis 框架用于操作数据,支持 SQL 语句,因此在体验 MyBatis 框架的时候,需要使用数据库配合进行测试。咱们在数据库中创建了一个名为“person”的表,并通过 MyBatis 框架对其进行一系列常见的操作(增、删、改、查等)。

3 配置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.cj.jdbc.Driver"/>
                <property name="url" value="jdbc:mysql://localhost:3306/ssm?serverTimezone=GMT%2B8"/>
                <property name="username" value="root"/>
                <property name="password" value="root"/>
            </dataSource>
        </environment>
    </environments>
    <!-- 加载映射文件 mapper -->
    <mappers>
        <!--加载配置文件-->
        <!-- 路径用 斜线(/) 分割,而不是用 点(.) -->
        <mapper resource="Mapper/personMapper.xml"></mapper>
    </mappers>
</configuration>

4 配置personMapper.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.xiaonuo.domain.Person"><!-- 命名空间,名字可以随意起,只要不冲突即可 -->
    <!-- 对象映射,可以不写 -->
    <!-- 查询功能,resultType 设置返回值类型 -->
    <!-- id是为了区别于其他的select 别重复 -->
    <!-- parameterType是设置传进来的类型-->
    <!--单个参数配置-->
    <select id="selectId" resultType="com.xiaonuo.domain.Person" parameterType="Long">
       select * from person where id = #{id}
    </select>
    <!--多个参数配置 因为刚刚好是一个类 所以可以直接封装在实体类-->
    <!--keyProperty 对应的主键的对象"-->
    <!--Mybatis 配置文件 useGeneratedKeys 参数只针对 insert 语句生效,默认为 false。当设置为 true 时,表示如果插入的表以自增列为主键,则允许 JDBC 支持自动生成主键,并可将自动生成的主键返回。-->
    <insert id="insertStudent" parameterType="com.xiaonuo.domain.Person" keyProperty="id" useGeneratedKeys="true">
       insert into person values(#{id},#{name},#{sex})
    </insert>
    <delete id="deleteById" parameterType="com.xiaonuo.domain.Person">
       delete from person where sex = #{sex}
    </delete>
    <update id="updateById" parameterType="com.xiaonuo.domain.Person">
       update person set name = #{name},sex = #{sex} where id = #{id}
    </update>
</mapper>

5 测试方法

public class test1 {
    //读取config.xml文件为了连数据库
    private Reader resourceAsReader = Resources.getResourceAsReader("config.xml");
    //加载配置文件
    private SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(resourceAsReader);
    //打开一共session连接
    private SqlSession sqlSession = sessionFactory.openSession();
    private final String statement = "com.xiaonuo.domain.Person.";

    public test1() throws IOException {
    }

    @Test
    public void selectById() throws IOException {
        Person person = sqlSession.selectOne(this.statement + "selectId", 1L);
        System.out.println(person.getSex());
        sqlSession.close();
    }
    @Test
    public void insert(){
        Person person2 = new Person();
        person2.setId(2L);
        person2.setName("cyc");
        person2.setSex('女');
        int insert = sqlSession.insert(statement + "insertStudent", person2);
        sqlSession.commit();
        sqlSession.close();
        System.out.println(insert);
    }
    @Test
    public void selectAll(){
        List<Person> personList = sqlSession.selectList(statement + "selectAll");
        System.out.println(personList);
    }
    @Test
    public void deleteById(){
        int delete = sqlSession.delete(statement + "deleteById","女");
        System.out.println(delete);
        sqlSession.commit();
        sqlSession.close();
    }
    @Test
    public void updateById(){
        Person person = sqlSession.selectOne(this.statement + "selectId", 2L);
        person.setSex('男');
        int update = sqlSession.update(statement + "updateById", person);
        sqlSession.commit();
        sqlSession.close();
    }
}

6.使用接口的方法进行映射

1.创建接口

package com.xiaonuo.Interface;

import com.xiaonuo.domain.Person;

import java.util.List;
<!--使用接口的创建的时候 xml中的namespace要选择是接口的路径 这样能保证唯一的对应-->
<!--方法的名字是 id的名字 可以联系起来-->
<!--注意的是就算返回是list 但是xml中配置的返回还是一个类 例如:com.xiaonuo.domain.Person-->
<!--参数也要和xml中对应起来-->
public interface PersonMapper {
    public Person selectId(Long id);
    public List<Person> selectAll();
    public Integer insertStudent(Person person);
    public Integer deleteById(Person person);
    public Integer updateById(Person Person);

}

2.测试(对比之前的使用测试类我们可以发现 我们不需要再自己一个个去找xml的id了 直接对应起来了)

//加载config.xml文件为了连数据库
private Reader resourceAsReader = Resources.getResourceAsReader("config.xml");
private SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(resourceAsReader);
private SqlSession sqlSession = sessionFactory.openSession();
PersonMapper mapper = sqlSession.getMapper(PersonMapper.class);
/*查询
Person person = mapper.selectId(2L);
System.out.println(person);*/
/*插入
Person person = new Person();
person.setName("胖虎");
person.setSex('女');
Integer integer = mapper.insertStudent(person);
System.out.println(integer);
sqlSession.commit();
sqlSession.close();*/
/*查询全部
List<Person> people = mapper.selectAll();
System.out.println(people);*/
/*删除
Person person = mapper.selectId(2L);
mapper.deleteById(person);
sqlSession.commit();
sqlSession.close();
*/
Person person = mapper.selectId(2L);
System.out.println(person);
person.setSex('男');
Integer integer = mapper.updateById(person);
System.out.println(integer);
sqlSession.commit();
sqlSession.close();

有时候查询list会是空 可能是编码问题 那么就在mysql连接上加上

?characterEncoding=utf-8
$符号会直接传一个不带双引号的参数进去 可以用模糊查询
#符号会直接传一个带引号的参数进去

猜你喜欢

转载自blog.csdn.net/qq_40632760/article/details/87902608
今日推荐