不说废话直接上代码,首先看下我的目录机构:
红色部分,表明你所需的jar包,已经配置文件.
创建用户表,以及插入数据.
create table books(id int (11) not null auto_increment, image_url varchar(100) not null, book_name varchar(50) not null, author varchar(20) not null, description varchar(255) not null, primary key (id))ENGINE=InnoDB DEFAULT CHARSET=utf8; insert into books(image_url,book_name,author,description) values('resources/images/english.jpg','考研英语','孙悟空','内容系统而全面,英语学习的好帮手!内容系统而全面,英语学习的好帮手!内容系统而全面,英语学习的好帮手!内容系统而全面,英语学习的好帮手!'); insert into books(image_url,book_name,author,description) values('resources/images/english.jpg','考研数学','刘诗诗','内容系统而全面,数学学习的好帮手,内容系统而全面,数学学习的好帮手内容系统而全面,数学学习的好帮手内容系统而全面,数学学习的好帮手');
创建Book.java
package com.mybatis.model; public class Book { private int id; private String imageUrl; private String name; private String author; private String description; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getImageUrl() { return imageUrl; } public void setImageUrl(String imageUrl) { this.imageUrl = imageUrl; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getAuthor() { return author; } public void setAuthor(String author) { this.author = author; } public String getDescription() { return description; } public void setDescription(String description) { this.description = description; } }
设置mybatis 配置文件:configuration.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> <typeAliases> <!-- 给实体类去一个别名 --> <typeAlias type="com.mybatis.model.Book" alias="Book"/> </typeAliases> <!-- 数据源配置,这里用MySQL数据库 --> <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/test"/> <property name="username" value="root"/> <property name="password" value="123456"/> </dataSource> </environment> </environments> <mappers> <!-- book.xml装载进来,等同于把Dao的实现类装载进来 --> <mapper resource="com/mybatis/model/book.xml"/> </mappers> </configuration>建立与Book.java对应的映射文件 book.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"> <!--这块等于Dao接口的实现 namespace必须和接口的类路径一样--> <mapper namespace="com.mybatis.model.BookMapper"> <!-- queryEmpById必须和接口中的方法名一样, 返回一个Emp 就是刚才的别名 ,如果不用别名要连路径一起写,麻烦--> <select id="selectBookById" parameterType="int" resultType="Book"> select * from books where id=#{id} </select> </mapper>最后就让我们编写一个测试类,运行我们的程序
package com.mybatis.test; import java.io.IOException; 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 com.mybatis.model.Book; public class Test { /*** * 获得MyBatis SqlSessionFactory * SqlSessionFactory 负责创建SqlSession ,一旦创建成功,就可以用SqlSession实例来执行映射语句 * ,commit,rollback,close等方法 * @return */ private static SqlSessionFactory getSessionFactory(){ SqlSessionFactory sessionFactory=null; String resource="configuration.xml"; try { sessionFactory=new SqlSessionFactoryBuilder().build(Resources.getResourceAsReader(resource)); } catch (IOException e) { e.printStackTrace(); } return sessionFactory; } public static void main(String[] args) { SqlSession session=getSessionFactory().openSession(); Book book=session.selectOne("com.mybatis.model.BookMapper.selectBookById", 2); System.out.println(book.getAuthor()); } }