先新建一个数据库:mybatis
粘贴sql语句来创建表和数据:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
DROP TABLE IF EXISTS `student`; CREATE TABLE `student` ( `id` int(11) NOT NULL auto_increment, `name` varchar(255) default NULL, `age` int(11) default NULL, `sex` varchar(255) default NULL, `classes` varchar(255) default NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8; INSERT INTO `student` VALUES ('1', '热健康', '52', '男', '三年级'); INSERT INTO `student` VALUES ('2', '李志祥', '15', '女', '四年级'); INSERT INTO `student` VALUES ('3', '苗磊', '18', '男', '五年级'); INSERT INTO `student` VALUES ('4', '李科', '11', '女', '六年级'); INSERT INTO `student` VALUES ('5', '姚凯', '22', '男', '七年级'); INSERT INTO `student` VALUES ('6', '李上', '15', '男', '八年级'); INSERT INTO `student` VALUES ('7', '李水', '19', '男', '九年级'); |
新建Java Project项目:MyBatis01_HelloWord
新建包:com.ml.mybatis
新建学生类Student
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
package com.ml.mybatis; public class Student { private int id; private String name; private String sex; private int age; private String classes; 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 String getSex() { return sex; } public void setSex(String sex) { this.sex = sex; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public String getClasses() { return classes; } public void setClasses(String classes) { this.classes = classes; } @Override public String toString() { return "Student [id=" + id + ", name=" + name + ", sex=" + sex + ", age=" + age + ", classes=" + classes + "]"; } } |
新建xml文件studentMapper.xml, 映射文件,也是放在com.ml.mybatis包里面
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
<?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.ml.mybatis.studentMapper"> <!-- namespace:名称空间;指定为接口的全类名 id:唯一标识 resultType:返回值类型 #{id}:从传递过来的参数中取出id值 --> <select id="selectStudent" resultType="com.ml.mybatis.Student"> select * from student where id= #{id} </select> </mapper> |
然后在建立一个配置文件夹config, 用来存放各种配置文件当然也可以不建立,放在项目文件夹些和src目录同级就行
注意:和src同级的文件夹需要Build Path一下,不然会出现找不到文件的情况
在config文件夹里 新建xml文件 mybatis-config.xml, mybatis的全局配置文件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
<?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://localhost:3306/mybatis?useUnicode=true&characterEncoding=utf-8"/> <property name="username" value="root" /> <property name="password" value="root" /> </dataSource> </environment> </environments> <!-- 将我们写好的sql映射文件(studentMapper.xml)注册到全局配置文件(mybatis-config.xml)中 --> <mappers> <mapper resource="com/ml/mybatis/studentMapper.xml" /><!--resource="sql映射文件目录"--> </mappers> </configuration> |
新建包com.ml.mybatis.test
然后新建测试类:MyBatisTest
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
package com.ml.mybatis.test; import java.io.IOException; import java.io.InputStream; 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 com.ml.mybatis.Student; public class MyBatisTest { public static void main(String[] args) throws IOException { String resource = "mybatis-config.xml"; InputStream inputStream = Resources.getResourceAsStream(resource); SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream); SqlSession openSession = sqlSessionFactory.openSession(); Student student=openSession.selectOne("com.ml.mybatis.studentMapper.selectStudent", 1); openSession.close(); System.out.println(student); } } |
Comments | NOTHING