标签:uil end shu develop rop query password local nts
学习链接:https://www.bilibili.com/video/BV1gs411j7kA?p=2
1.建立java文件
2.加载类路径。mybatis.jar 和 数据库驱动jar
File->Project Structure->Modules->Dependencies->右侧加号->1.jar
3.在数据库中建立一张person表 id name age
在src中 建立一个person类
4.通过配置文件将类和表对应起来,在person所在的那个类里面
建立一个 personMapper.xml文件
1)personMapper.xml所在的路径 没有.xml
通过id值 返回值类型 然后里面需要传入的参数的类型
5.在src下面直接建立 conf.xml文件
里面有那个url username password mapper 里面是映射文件的路径
这里需要将数据库的链接包导入进来
4.测试类TestMyBatis文件
加载conf.xml文件 配置文件 为了访问数据库
然后通过id进行访问
testMyBatis package org.student.entity; 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.Reader; public class testMyBatis { public static void main(String[] args) throws IOException { //加载MyBatis配置文件(为了访问数据库) Reader reader = Resources.getResourceAsReader("conf.xml") ; SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(reader) ; //session - connection SqlSession session = sessionFactory.openSession() ; String statement = "org.student.entity.personMapper.queryPersonById" ; Person person = session.selectOne( statement,4 ) ; System.out.println(person); session.close(); } }
配置文件 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"/> <dataSource type="POOLED"> <!-- 配置数据库信息 --> <property name="driver" value="com.mysql.cj.jdbc.Driver"/> <property name="url" value="jdbc:MySQL://localhost:3306/hotel?&useSSL=false&serverTimezone=UTC"/> <property name="username" value="root"/> <property name="password" value="root"/> </dataSource> </environment> </environments> <mappers> <!-- 加载映射文件 --> <mapper resource="org/student/entity/personMapper.xml"/> </mappers> </configuration>
personMapper.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="org.student.entity.personMapper"> <select id="queryPersonById" resultType="org.student.entity.Person" parameterType="int"> select * from person where id = #{id} </select> </mapper>
person类 package org.student.entity; public class Person { private int id; private String name; private int age; public Person() { } public Person(int id, String name, int age) { this.id = id; this.name = name; this.age = age; } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } @Override public String toString() { return this.id+","+this.name+","+this.age ; } }
标签:uil end shu develop rop query password local nts
原文地址:https://www.cnblogs.com/ejwbytshooting/p/12638178.html