王者系列之Mybatis入门配置(maven版本)

这次我们一起来做一个入门级别的持久层框架Mybatis的配置,正所谓完事开头难。

1 引入依赖

     <dependency>
            <groupId>org.mybatisgroupId>
            <artifactId>mybatisartifactId>
            <version>3.2.6version>
        dependency>

        <dependency>
            <groupId>mysqlgroupId>
            <artifactId>mysql-connector-javaartifactId>
            <version>5.1.38version>
        dependency>

       //打印日志用
       <dependency>
            <groupId>log4jgroupId>
            <artifactId>log4jartifactId>
            <version>1.2.17version>
        dependency>


        //测试用
       <dependency>
            <groupId>junitgroupId>
            <artifactId>junitartifactId>
            <version>3.8.1version>
            <scope>testscope>
        dependency>

 
        <plugins>
            <plugin>
                
                <groupId>org.apache.maven.pluginsgroupId>
                <artifactId>maven-compiler-pluginartifactId>
                <version>3.5.1version>
                <configuration>
                    
                    <source>1.8source>
                    <target>1.8target>
                    
                    <encoding>UTF-8encoding>
                configuration>
            plugin>
        plugins>

         <resources>
             <resource>
                 
                 <directory>src/main/javadirectory>
                 <includes>
                     
                     <include>**/*.classinclude>
                     <include>**/*.xmlinclude>
                 includes>

             resource>

         resources>

2 配置mybatis的配置文件

<configuration>
//这些标签都是有固定顺序的,这个阿帕奇要求的,用别人的东西,当然要遵守别人的游戏规则。

//引入外部配置文件,下面的${jdbc.dataSource}这些就是获取外部配置文件的值,这样做的好处就是不用每次来修改mybatis的配置文件,只要在外部得properties文件中配置即可;
    <properties resource="settings.properties"/>

    
    <environments default="development">
        <environment id="development">
            
            <transactionManager type="${jdbc.transactionManager}"/>
            
            <dataSource type="${jdbc.dataSource}">
                <property name="driver" value="${jdbc.driver}"/>
                <property name="url"
                          value="${jdbc.url}"/>
                <property name="username" value="${jdbc.username}"/>
                <property name="password" value="${jdbc.password}"/>
            dataSource>
        environment>
    environments>

    
    <mappers>
        <mapper resource="com/quyang/my/sqlmap/BaseUserMap.xml"/>
    mappers>
configuration>

3 编写sql文件



        //这里的namespace最好用接口的全路径,实际开发中大多都是面向接口的方式。这里写接口的全路径就是指接口和这个sql文件进行了绑定。
<mapper namespace="com.quyang.my.map.BaseUserMapa">

//查询语句,这个文件怎么来写,这里就不多说了,很简单的,不懂百度
    <select id="selectBaseUserById" parameterType = "Integer" resultType="com.quyang.my.BaseUser">
        select * from base_user where user_id = #{iddd}
    select>
mapper>

4 外部配置文件

jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/user?characterEncoding=utf-8
jdbc.username=root
jdbc.password=123456
jdbc.transactionManager=JDBC
jdbc.dataSource=POOLED

可以和上面的mybatis的配置文件对照来看。

5 日志文件配置

# Global logging configuration 输出debug级别以上
log4j.rootLogger=DEBUG, stdout
# Console output... log输出到控制台,ConsoleAppender表示log输出到控制台
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
# log布局
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
# 输入格式
log4j.appender.stdout.layout.ConversionPattern=%5p [%t] - %m%n

6 测试

  public void testApp() {
        try {
        //首先就是要建在mybatis的配置文件
            InputStream resourceAsStream = Resources.getResourceAsStream("SqlMapConfig.xml");
            SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(resourceAsStream);
            SqlSession sqlSession = sessionFactory.openSession();
            //获取接口的实例,实质是一个代理对象;
            BaseUserMapa selectOne = sqlSession.getMapper(BaseUserMapa.class);
            //调用接口的方法查表
            BaseUser baseUser = selectOne.selectBaseUserById(10000);
            System.out.println("AppTest.testApp=" + baseUser.getName());

        } catch (IOException e) {
            e.printStackTrace();
            System.out.println("AppTest.error");
        }
    }

7 结果

E:\jdk\set\bin\java -ea -Didea.test.cyclic.buffer.size=1048576 -javaagent:D:\ideaUI\lib\idea_rt.jar=49322:D:\ideaUI\bin -Dfile.encoding=UTF-8 -classpath D:\ideaUI\lib\idea_rt.jar;D:\ideaUI\plugins\junit\lib\junit-rt.jar;D:\ideaUI\plugins\junit\lib\junit5-rt.jar;E:\jdk\set\jre\lib\charsets.jar;E:\jdk\set\jre\lib\deploy.jar;E:\jdk\set\jre\lib\ext\access-bridge-64.jar;E:\jdk\set\jre\lib\ext\cldrdata.jar;E:\jdk\set\jre\lib\ext\dnsns.jar;E:\jdk\set\jre\lib\ext\jaccess.jar;E:\jdk\set\jre\lib\ext\jfxrt.jar;E:\jdk\set\jre\lib\ext\localedata.jar;E:\jdk\set\jre\lib\ext\nashorn.jar;E:\jdk\set\jre\lib\ext\sunec.jar;E:\jdk\set\jre\lib\ext\sunjce_provider.jar;E:\jdk\set\jre\lib\ext\sunmscapi.jar;E:\jdk\set\jre\lib\ext\sunpkcs11.jar;E:\jdk\set\jre\lib\ext\zipfs.jar;E:\jdk\set\jre\lib\javaws.jar;E:\jdk\set\jre\lib\jce.jar;E:\jdk\set\jre\lib\jfr.jar;E:\jdk\set\jre\lib\jfxswt.jar;E:\jdk\set\jre\lib\jsse.jar;E:\jdk\set\jre\lib\management-agent.jar;E:\jdk\set\jre\lib\plugin.jar;E:\jdk\set\jre\lib\resources.jar;E:\jdk\set\jre\lib\rt.jar;D:\eclipse\myweb\testmybatis\target\test-classes;D:\eclipse\myweb\testmybatis\target\classes;D:\repo\repository\junit\junit\3.8.1\junit-3.8.1.jar;D:\repo\repository\asm\asm\3.3.1\asm-3.3.1.jar;D:\repo\repository\cglib\cglib\2.2\cglib-2.2.jar;D:\repo\repository\commons-logging\commons-logging\1.2\commons-logging-1.2.jar;D:\repo\repository\javassist\javassist\3.11.0.GA\javassist-3.11.0.GA.jar;D:\repo\repository\log4j\log4j\1.2.17\log4j-1.2.17.jar;D:\repo\repository\org\apache\logging\log4j\log4j-api\2.5\log4j-api-2.5.jar;D:\repo\repository\org\apache\logging\log4j\log4j-core\2.5\log4j-core-2.5.jar;D:\repo\repository\org\mybatis\mybatis\3.2.6\mybatis-3.2.6.jar;D:\repo\repository\mysql\mysql-connector-java\5.1.38\mysql-connector-java-5.1.38.jar;D:\repo\repository\org\slf4j\slf4j-api\1.7.18\slf4j-api-1.7.18.jar;D:\repo\repository\org\slf4j\slf4j-log4j12\1.7.7\slf4j-log4j12-1.7.7.jar com.intellij.rt.execution.junit.JUnitStarter -ideVersion5 -junit3 com.quyang.my.AppTest,testApp
DEBUG [main] - Logging initialized using 'class org.apache.ibatis.logging.slf4j.Slf4jImpl' adapter.
DEBUG [main] - PooledDataSource forcefully closed/removed all connections.
DEBUG [main] - PooledDataSource forcefully closed/removed all connections.
DEBUG [main] - PooledDataSource forcefully closed/removed all connections.
DEBUG [main] - PooledDataSource forcefully closed/removed all connections.
DEBUG [main] - Opening JDBC Connection
Sun Aug 20 19:29:46 GMT+08:00 2017 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.
DEBUG [main] - Created connection 1899073220.
DEBUG [main] - Setting autocommit to false on JDBC Connection [com.mysql.jdbc.JDBC4Connection@71318ec4]
DEBUG [main] - ==>  Preparing: select * from base_user where user_id = ? 
DEBUG [main] - ==> Parameters: 10000(Integer)
DEBUG [main] - <==      Total: 1
AppTest.testApp=13738860434

Process finished with exit code 0

可以看出,log4j将所有的信息都打印出来了,包括查询标的select语句和参数,结果,13738860434这个就是我查出来的本地mysql数据库的数据;

8 要注意的问题

要搞清楚类路径是什么,一般我们写一个类的话,文件最上面会出现com.xxx.xxx,这就是文件的路径,而要写一个类路径下的文件的话,写出来的效果是,文件做上面啥都没有,也就是说没有包名,就是在根目录下。

你可能感兴趣的:(王者系列之Mybatis入门配置(maven版本))