MyBatis-新建一个项目

  • 前言
    •   看视频笔记。
  • 编译环境:MyEclipse+MySql5.7+JDK1.8
  • 目的:新建一个可运行的MyBatis项目。
  • 视频:https://www.bilibili.com/video/BV1gs411j7kA?from=search&seid=11768494494103305445
  • mybatis的下载地址:https://github.com/mybatis/mybatis-3/releases
  • 存在问题:老师代码里面,包括我在idea编辑器里面都可以用这个selectOne(statement,1),但是在我自己的MyEclipse只能用selectOne(statement,Integer.valueOf(1)),而且还需要强制转换。
    • 这个问题已经知道为什么了。selectOne这个方式是旧版本的Mybatis提供的操作方式,现在也可以正常工作。还有另一种方式是通过mapper接口实现,也是Mybatis官方推荐使用的,表达方式也更加直白,代码更加清晰,类型更加安全,也不用担心易错的字符串字面值以及强制类型转换。
    • 更重要的问题我已经找到就是我MyEclipse的版本太版本太低了,mybatis3.5以上最低支持jdk1.8。如果用成3.5以下的版本就可以了。
    • 查看的话:点开项目的properties-》Java Compiler查看
    •  
  • 步骤:
    • 需要两个jar包:mybatis-3.5.6.jar;mysql-connector-java-5.1.41.jar
    • 配置conf.xml和新建表
    • 写personMapper.xml和Person.class
    • 写测试文件
  •   文件整体情况:Java文件
  •  conf.xml代码:视频老师用的是Oracle

<?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:3360/Test?useSSL=false"/>
             <property name="username" value="root"/>
             <property name="password" value="Admin+123"/>
             </dataSource>
         </environment>
     </environments>
     <mappers>
         <mapper resource="org/lanqiao/entity/personMapper.xml"/>
     </mappers>
</configuration>
  • 新建表
CREATE TABLE `person` (
  `id` tinyint(4) DEFAULT NULL,
  `name` varchar(20) DEFAULT NULL,
  `age` tinyint(4) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
INSERT INTO person VALUES(1,"zs",24);
  • 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="org.lanqiao.entity.personMapper">
            <select id="queryPersonById" resultType="org.lanqiao.entity.Person" parameterType="int">
         select * from person where id =#{id}
         </select>
</mapper>
  • Person.java
package org.lanqiao.entity;

public class Person {
    public Person() {
    }
    public Person(int id, String name, int age) {

        this.id = id;
        this.name = name;
        this.age = age;
    }
    
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    }
    
    
    public String toString() {
        return "Person [id=" + this.id + ", name=" + this.name + ", age=" + this.age + "]";
    }
  • TestMyBatis.java
  • package org.lanqiao.entity;
    
    import java.io.IOException;
    import java.io.Reader;
    
    import org.apache.ibatis.io.Resources;
    import org.apache.ibatis.session.SqlSession;
    import org.apache.ibatis.session.SqlSessionFactory;
    import org.apache.ibatis.session.SqlSessionFactoryBuilder;
    
    
    
    public class TestMyBatis {
        public static void main(String[] args) throws IOException{
            //加载MyBatis文件(为了访问数据库)
            Reader reader=Resources.getResourceAsReader("conf.xml");
            // SqlsessionFactory - connection
            SqlSessionFactory sessionFactory=new SqlSessionFactoryBuilder().build(reader);
            //session - connection
            SqlSession session=sessionFactory.openSession();
            String statement="org.lanqiao.entity.personMapper.queryPersonById";
            Person person= (Person) session.selectOne(statement,Integer.valueOf(1));
            System.out.println(person);
            session.close();
        }
    
    }

     

posted @ 2021-03-08 20:50  陈遛狗  阅读(89)  评论(0编辑  收藏  举报