最近打算读读mybatis的源码来提高自己写bug的水平,hahaha
找到网站下载源码:https://github.com/mybatis/mybatis-3
使用idea进行关联,并且配置好环境之后。
配合中文文档进行搭建:
1.导入jdbc包,我用的 lombok
<!-- https://mvnrepository.com/artifact/org.projectlombok/lombok --> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <version>1.16.18</version> <scope>provided</scope> </dependency> <!--没有mysql依赖--> <!-- 数据库连接池、驱动 --> <!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java--> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.8</version> </dependency>
2.创建数据库连接的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.jdbc.Driver"/> <property name="url" value="jdbc:mysql://127.0.0.1:3306/mybatis"/> <property name="username" value="root"/> <property name="password" value="root"/> </dataSource> </environment> </environments> <mappers> <mapper resource="mybatis\UserMapper.xml"/> </mappers> </configuration>
3.创建java对象
package java7115; import lombok.*; @Data @Builder//链式调用<-- @RequiredArgsConstructor @AllArgsConstructor @ToString public class User { private Integer id; private String username; private String password; private String remarks; private String true_name; }
4.编写mybatis的sql
<?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="java7115"> <select id="selectUser" resultType="java7115.User"> select * from User where id = #{id} </select> </mapper>
5.
为了能显示sql增加日志
log4j.properties
# # Copyright 2009-2016 the original author or authors. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. # ### Global logging configuration log4j.rootLogger=debug, stdout ### Uncomment for MyBatis logging log4j.logger.org.apache.ibatis=ERROR log4j.logger.org.apache.ibatis.session.AutoMappingUnknownColumnBehavior=WARN, lastEventSavedAppender ### Console output... log4j.appender.stdout=org.apache.log4j.ConsoleAppender log4j.appender.stdout.layout=org.apache.log4j.PatternLayout log4j.appender.stdout.layout.ConversionPattern=%5p [%t] - %m%n log4j.appender.lastEventSavedAppender=org.apache.ibatis.session.AutoMappingUnknownColumnBehaviorTest$LastEventSavedAppender
main方法测试
package org.apache.ibatis; import java7115.User; import lombok.extern.slf4j.Slf4j; import org.apache.ibatis.io.Resources; import org.apache.ibatis.session.SqlSession; import org.apache.ibatis.session.SqlSessionFactory; import org.apache.ibatis.session.SqlSessionFactoryBuilder; import java.io.IOException; import java.io.InputStream; public class Main { public static void main(String[] args) throws IOException { String resource = "mybatis-config.xml"; //读取配置文件 InputStream inputStream = Resources.getResourceAsStream(resource); //创建连接工厂 SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream); //得到session实例 SqlSession session = sqlSessionFactory.openSession(); try { //根据命名空间找方法 User blog = (User) session.selectOne("java7115.selectUser", 1); System.out.println(blog); } finally { session.close(); } } }
输出结果:
DEBUG [main] - ==> Preparing: select * from User where id = ?
DEBUG [main] - ==> Parameters: 1(Integer)
DEBUG [main] - <== Total: 1
User(id=1, username=admin, password=123, remarks=管理员, true_name=王大陆)
这个是我当前目录图片