MyBatis学习笔记(一)先查出个东西呗

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

当前用到的jar包

mybatis-3.4.6.jar

mysql-connector-java-8.0.11.jar

两个都是maven下载的, 推荐一下。

测试项目文件结构

第一步、建了个普通的javaEE项目,添加了lib包。

第二步、新建了mybatis-confing.xml,官网的描述是:“XML 配置文件(configuration XML)中包含了对 MyBatis 系统的核心设置,包含获取数据库连接实例的数据源(DataSource)和决定事务作用域和控制方式的事务管理器(TransactionManager)。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/XXX?useUnicode=true&amp;characterEncoding=utf-8&amp;allowMultiQueries=true&amp;useAffectedRows=true&amp;serverTimezone=Asia/Shanghai"/>
                <property name="username" value="root"/>
                <property name="password" value="root"/>
            </dataSource>
        </environment>
    </environments>
    <mappers>
        <mapper resource="org/mybatis/example/user.xml"/>
    </mappers>
</configuration>

需要注意的是:driver的值是跟着mysql的驱动jar包变的;url问号后面的后缀包含了解决中文乱码、时区啥的,日后研究。

这个xml也不是必须的,官网又提供了不使用xml构建SqlSessionFactory的方式示例:

DataSource dataSource = BlogDataSourceFactory.getBlogDataSource();
TransactionFactory transactionFactory = new JdbcTransactionFactory();
Environment environment = new Environment("development", transactionFactory, dataSource);
Configuration configuration = new Configuration(environment);
configuration.addMapper(BlogMapper.class);
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(configuration);

第三步、创建映射器文件 user.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.mybatis.example.UserMapper">
    <select id="findAll"   resultType="org.mybatis.example.User">
        SELECT * FROM `sys_user`
    </select>
</mapper>
parameterType节点是参数类型,可以是实体类全路径、原有的类型(map、String等),resultType就是返回的类型,参数和parameterType一样, id就像方法名一样。

映射器文件也不是必须创建的,也可以用映射器类代替

package org.mybatis.example;
public interface BlogMapper {
  @Select("SELECT * FROM blog WHERE id = #{id}")
  Blog selectBlog(int id);
}

不过官网提示,对于复杂的sql,就不好整了, 建议还是用xml好一些。

第四步、调用查询,解释不了,复制就对了。

 public static void main(String[] arg) {
        try {
            String resource = "org/mybatis/example/mybatis-config.xml";
            InputStream inputStream = Resources.getResourceAsStream(resource);
            SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
            SqlSession session = sqlSessionFactory.openSession();

            List<User> user = session.selectList("org.mybatis.example.UserMapper.findAll", 1);

            System.out.println(user.size());


        } catch (IOException e) {
            e.printStackTrace();
        }
    }

运行结果:

Sun Dec 09 13:20:52 CST 2018 WARN: Establishing SSL connection without server's identity verification is not recommended. According to MySQL 5.5.45+, 5.6.26+ and 5.7.6+ requirements SSL connection must be established by default if explicit option isn't set. For compliance with existing applications not using SSL the verifyServerCertificate property is set to 'false'. You need either to explicitly disable SSL by setting useSSL=false, or set useSSL=true and provide truststore for server certificate verification.
1951

Process finished with exit code 0

这警告也不知道是啥意思~~

猜你喜欢

转载自blog.csdn.net/baitianmingdebai/article/details/84932033
今日推荐