标签:
1、创建一个Java project,JikeBook
2、添加项目所需的依赖包
如 mybatis-3.2.8.jar,是实现mybatis功能所必需的依赖包
mysql-connector-java-3.0.17-ga-bin.jar是连接mysql数据库的依赖包,
log4j是日志输出依赖包
3、在src目录下创建各个package
4、首先创建log4j.properties
1 log4j.rootLogger=DEBUG, Console 2 3 #Console 4 log4j.appender.Console=org.apache.log4j.ConsoleAppender 5 log4j.appender.Console.layout=org.apache.log4j.PatternLayout 6 log4j.appender.Console.layout.ConversionPattern=%d [%t] %-5p [%c] - %m%n 7 8 log4j.logger.java.sql.ResultSet=INFO 9 log4j.logger.org.apache=INFO 10 log4j.logger.java.sql.Connection=DEBUG 11 log4j.logger.java.sql.Statement=DEBUG 12 log4j.logger.java.sql.PreparedStatement=DEBUG
将一些日志的输出更改为DEBUG,才能输出相关日志信息
5、在jike.book.map包下创建jikeUser.xml
1 <?xml version="1.0" encoding="UTF-8" ?> 2 3 4 <!DOCTYPE mapper 5 PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" 6 "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> 7 8 <mapper namespace="/"> 9 10 <select id="findById" parameterType="int" resultType="jike.book.pojo.JiKeUser"> 11 select * from jikeUser where id=#{id} 12 </select> 13 14 15 </mapper>
6、在jike.book.map包下创建MyBatisConfig.xml
1 <?xml version="1.0" encoding="UTF-8" ?> 2 3 4 <!DOCTYPE configuration 5 PUBLIC "-//mybatis.org//DTD Config 3.0//EN" 6 "http://mybatis.org/dtd/mybatis-3-config.dtd"> 7 8 <configuration> 9 10 <environments default="development"> 11 <environment id="development"> 12 <transactionManager type="JDBC"> 13 </transactionManager> 14 <dataSource type="POOLED"> 15 <property name="driver" value="com.mysql.jdbc.Driver"/> 16 <property name="url" value="jdbc:mysql://localhost:3306/jikebook"/> 17 <property name="username" value="root"/> 18 <property name="password" value=""/> 19 </dataSource> 20 </environment> 21 </environments> 22 23 <mappers> 24 <mapper resource="jike/book/map/jikeUser.xml"/> 25 </mappers> 26 27 </configuration>
7、在jike.book.pojo下创建JiKeUser.java
1 package jike.book.pojo; 2 3 public class JiKeUser { 4 private int id; 5 private String userName; 6 private String password; 7 public int getId() { 8 return id; 9 } 10 public void setId(int id) { 11 this.id = id; 12 } 13 public String getUserName() { 14 return userName; 15 } 16 public void setUserName(String userName) { 17 this.userName = userName; 18 } 19 public String getPassword() { 20 return password; 21 } 22 public void setPassword(String password) { 23 this.password = password; 24 } 25 }
8、在jike.book.test下创建TestHello.java
1 package jike.book.test; 2 3 import java.io.IOException; 4 import java.io.Reader; 5 6 import jike.book.pojo.JiKeUser; 7 8 import org.apache.ibatis.io.Resources; 9 import org.apache.ibatis.session.SqlSession; 10 import org.apache.ibatis.session.SqlSessionFactory; 11 import org.apache.ibatis.session.SqlSessionFactoryBuilder; 12 13 public class TestHello { 14 15 public static void main(String[] args) { 16 17 String resource = "jike/book/map/MyBatisConfig.xml"; 18 Reader reader = null; 19 SqlSession session; 20 try { 21 reader = Resources.getResourceAsReader(resource); 22 } catch (IOException e) { 23 e.printStackTrace(); 24 } 25 SqlSessionFactory sqlMapper = new SqlSessionFactoryBuilder() 26 .build(reader); 27 session = sqlMapper.openSession(); 28 JiKeUser temp = session.selectOne("findById", 1); 29 System.out.println(temp.getUserName()); 30 session.close(); 31 } 32 33 }
最后运行项目:
得到了数据库中的数据
标签:
原文地址:http://www.cnblogs.com/UniqueColor/p/5753162.html