第一个mybatis程序的搭建及测试
第一个mybatis程序
(1)加入mybatis类库:mybatis-3.2.2.jar
数据库驱动程序:mysql-connector-java-5.1.8.jar
(2)创建实体类:com.seven.entities.Book.java
package com.seven.entities;
public class Book {
private Integer id;
private String name;
private String author;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
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;
}
@Override
public String toString() {
return "Book [id=" + id + ", name=" + name + ", author=" + author + "]";
}
}
(3)创建数据访问层接口:com.seven.dao.bookDao.java
package com.seven.dao;
import java.util.List;
import com.seven.entities.Book;
public interface bookDao {
public Book getBookById(Integer id);
public List<Book> getAllBook();
}
(4)创建SQL映射文件:com.seven.dao.bookDao.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="com.wanbangee.dao.BookDao">
<select id="getBookById" resultType="com.wanbangee.entities.Book">
<!-- 定義sql語句,並使用#{}接受傳遞的參數 -->
select * from book where id = #{id}
</select>
</mapper>
(5)创建mybatis全局配置文件:mybatis-conf.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">
<property name="" value="" />
</transactionManager>
<dataSource type="POOLED">
<property name="driver" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost:3306/mybatis" />
<property name="username" value="root" />
<property name="password" value="admin" />
</dataSource>
</environment>
</environments>
<!-- 講sql映射文件註冊到mybatis的全局配置文件中 -->
<mappers>
<!-- 将SQL映射文件注册到mybatis全局配置文件中-->
<!-- <mapper resource="com/wanbangee/dao/CountryDao.xml"/> -->
<!-- 注册SQL映射,注册的内容为接口的全类名。要求:SQL映射文件和接口在同一个包中 -->
<!-- <mapper class="com.wanbangee.dao.CountryDao"/> -->
<!-- 注册包中的所有映射文件(注解方式 )-->
<package name="com.wanbangee.dao"></package>
</mappers>
</configuration>
(6)创建测试类
public class Test02 {
@Test
public void testDao() throws Exception{
//讀取全局配置文件
String resource = "MyBatis-conf.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);
//通過全局配置文件的字節輸入流構建SqlSessionFactory對象
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
//打開一個新的SqlSession,相當于是一次對話,使用完需要關閉
//表示調用哪條sql語句,SQL語句需要傳遞的參數
SqlSession sqlSession = sqlSessionFactory.openSession();
//获取接口的动态代理对象
BookDao bookDao = sqlSession.getMapper(BookDao.class);
//调用接口中的方法
Book book = bookDao.getBookById(2);
//List<Country> country = countryDao.getAllCountry();
System.out.println(book);
sqlSession.close();
}
}
项目框架图
本文来自博客园,作者:藤原豆腐渣渣,转载请注明原文链接:https://www.cnblogs.com/javafufeng/p/14433505.html