Mybatis_Class_No.1
Posted MESGOD纯贱
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Mybatis_Class_No.1相关的知识,希望对你有一定的参考价值。
IBookDao
package MESGOD.dao.dao; import MESGOD.dao.entity.Book; import java.util.List; public interface IBookDao { public List<Book>findAllBooks(); }
IBookDao.xml
1 <?xml version="1.0" encoding="UTF-8" ?> 2 <!DOCTYPE mapper 3 PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" 4 "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> 5 6 7 <mapper namespace="MESGOD.dao.dao.IBookDao"> 8 <select id="findAllBooks" resultType="MESGOD.dao.entity.Book"> 9 select * from book 10 </select> 11 </mapper>
Book
package MESGOD.dao.entity; public class Book { private Integer bookID; private String bookName; private String bookAuthor; private Integer bookPrice; public Integer getBookID() { return bookID; } public void setBookID(Integer bookID) { this.bookID = bookID; } public String getBookName() { return bookName; } public void setBookName(String bookName) { this.bookName = bookName; } public String getBookAuthor() { return bookAuthor; } public void setBookAuthor(String bookAuthor) { this.bookAuthor = bookAuthor; } public Integer getBookPrice() { return bookPrice; } public void setBookPrice(Integer bookPrice) { this.bookPrice = bookPrice; } }
Test
package MESGOD.dao.Test; import MESGOD.dao.entity.Book; 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; import java.util.List; public class Test { public static void main(String[] args) { String path="mybatis-config.xml"; //InputStream inputStream=null; try { InputStream is=Resources.getResourceAsStream(path); //MyBatis 为了 访问数据 库 SqlSessionFactoryBuilder builder=new SqlSessionFactoryBuilder(); //SessionFactory SqlSessionFactory factory=builder.build(is); SqlSession session = factory.openSession(); List<Book> list = session.selectList("findAllBooks"); for (Book book:list){ System.out.println(book.getBookName()+"\\t"+book.getBookAuthor()); } session.close(); }catch (IOException e){ e.printStackTrace(); } } }
mybatis-config.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:///study"/> <property name="username" value="root"/> <property name="password" value=""/> </dataSource> </environment> </environments> <mappers> <mapper resource="MESGOD/dao/dao/IBookDao.xml"/> </mappers> </configuration>
数据库
/* SQLyog v10.2 MySQL - 5.6.24 : Database - study ********************************************************************* */ /*!40101 SET NAMES utf8 */; /*!40101 SET SQL_MODE=\'\'*/; /*!40014 SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0 */; /*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */; /*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE=\'NO_AUTO_VALUE_ON_ZERO\' */; /*!40111 SET @OLD_SQL_NOTES=@@SQL_NOTES, SQL_NOTES=0 */; CREATE DATABASE /*!32312 IF NOT EXISTS*/`study` /*!40100 DEFAULT CHARACTER SET utf8 */; USE `study`; /*Table structure for table `book` */ DROP TABLE IF EXISTS `book`; CREATE TABLE `book` ( `bookId` int(11) NOT NULL AUTO_INCREMENT, `bookName` varchar(50) DEFAULT NULL, `bookAuthor` varchar(50) DEFAULT NULL, `bookPrice` int(11) DEFAULT NULL, PRIMARY KEY (`bookId`) ) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8; /*Data for the table `book` */ insert into `book`(`bookId`,`bookName`,`bookAuthor`,`bookPrice`) values (1,\'吉安娜的抉择\',\'卡莉。费奥林啊\',100),(2,\'思绪飘零\',\'微冷的雨\',200),(3,\'平凡的世界\',\'路遥\',300); /*!40101 SET SQL_MODE=@OLD_SQL_MODE */; /*!40014 SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS */; /*!40014 SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS */; /*!40111 SET SQL_NOTES=@OLD_SQL_NOTES */;
以上是关于Mybatis_Class_No.1的主要内容,如果未能解决你的问题,请参考以下文章