简单使用mybatis(idea中使用)
首先创建一个maven项目
第一步:在pom.xml中添加依赖
<dependencies> <!--mybatis--> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.4.6</version> </dependency> <!--数据库相关 jar 包--> <dependency> <groupId>org.mariadb.jdbc</groupId> <artifactId>mariadb-java-client</artifactId> <version>2.3.0</version> </dependency> <!-- JUnit单元测试工具 --> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.10</version> </dependency> </dependencies>
第二步:创建数据库表
CREATE TABLE BOOK( ID INT AUTO_INCREMENT PRIMARY KEY, NAME VARCHAR(20), NUMBER int ); insert into BOOK(NAME,NUMBER) VALUES ('Java程序设计',10), ('数据结构',10), ('设计模式',10)
第三步:在main目录下的java源代码写代码
实体类Book
package com.wbg.entity; public class Book { int id; String name; int number; @Override public String toString() { return "Book{" + "id=" + id + ", name='" + name + '\'' + ", number=" + number + '}'; } public int getId() { return id; } public void setId(int id) { this.id = id; } public Book() { } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getNumber() { return number; } public void setNumber(int number) { this.number = number; } public Book(int id, String name, int number) { this.id = id; this.name = name; this.number = number; } }
bookMapper接口
package com.wbg.dao; import com.wbg.entity.Book; public interface bookMapper { Book selectBookById(); }
BookDao
package com.wbg.dao; import com.wbg.entity.Book; import org.apache.ibatis.session.SqlSession; import org.apache.ibatis.session.SqlSessionFactory; import org.apache.ibatis.session.SqlSessionFactoryBuilder; import java.io.InputStream; public class BookDao { public Book getBookById(int id){ //使用类加载器加载mybatis的配置文件 InputStream inputStream=BookDao.class.getClassLoader().getResourceAsStream("config.xml"); //构建sqlSession的工厂 SqlSessionFactory sqlSessionFactory=new SqlSessionFactoryBuilder().build(inputStream); //创建能执行映射文件中的sql SqlSession sqlSession=sqlSessionFactory.openSession(); Book book=sqlSession.selectOne("com.wbg.dao.bookMapper.selectBookById",2); return book; } public static void main(String[] args) { BookDao bookDao=new BookDao(); System.out.println(bookDao.getBookById(1)); } }
第四步:在resources中创建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="org.mariadb.jdbc.Driver"/> <property name="url" value="jdbc:mariadb://localhost:3306/stu"/> <property name="username" value="root"/> <property name="password" value="123456"/> </dataSource> </environment> </environments> <mappers> <mapper resource="mapper/bookMapper.xml"/> </mappers> </configuration>
第五步:在resources中创建mapper目录,在目录下创建bookMapper.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.wbg.dao.bookMapper"> <select id="selectBookById" resultType="com.wbg.entity.Book"> select * from Book where id=#{id} </select> </mapper>
测试
项目下载地址:https://github.com/weibanggang/mybatistest