1. 程式人生 > >MyBatis學習筆記(一)先查出個東西唄

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

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