标签:cli opened 依赖 ble 资源文件 input 加载 new localhost
首先创建一个maven项目
<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)
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; } }
package com.wbg.dao; import com.wbg.entity.Book; public interface bookMapper { Book selectBookById(); }
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)); } }
<?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>
<?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>
标签:cli opened 依赖 ble 资源文件 input 加载 new localhost
原文地址:https://www.cnblogs.com/weibanggang/p/9786178.html