实现学生信息的插入
- 安装Mybatis
将相关开发包引入项目中
- 定义实体类
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
| package com.node.domain;
public class Student { private Long id; private String name; private Integer age; public Long getId() { return id; } public void setId(Long id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } @Override public String toString() { return "Student [id=" + id + ", name=" + name + ", age=" + age + "]"; } }
|
- 定义Dao接口以及接口的实现类,并在接口中定义添加学生的方法
1 2 3 4
| public interface StudentDao {
public void insertStudent(Student student); }
|
- 创建配置文件mybatis.xml
在src目录下创建MyBatis的核心配置文件mybatis.xml
为主配置文件添加头文件约束
在主配置文件中定义MyBatis的运行环境(数据库连接)
在主配置文件中引入SQL映射文件
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
| <?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="mysql">
<environment id="mysql">
<transactionManager type="JDBC"></transactionManager>
<dataSource type="POOLED"> <property name="driver" value="com.mysql.jdbc.Driver"/> <property name="url" value="jdbc:mysql://localhost:3306/mybatis"/> <property name="username" value="root"/> <property name="password" value="mysql"/> </dataSource> </environment> </environments> <mappers>
<mapper resource="com/node/dao/StudentDao.xml"/> </mappers> </configuration>
|
- 编写SQL映射文件StudentDao.xml
在Dao接口的同级别目录中创建SQL映射文件:StudentDao.xml
为SQL映射文件添加头文件约束
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| <?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="test">
<insert id="insertStudent">
insert into student(name,age) values(#{name},#{age}) </insert> </mapper>
|
- 接口的实现类
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
| 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;
public class StudentDaoImpl implements StudentDao{
@Override public void insertStudent(Student student) { InputStream is=null;
try { is = Resources.getResourceAsStream("mybatis.xml"); } catch (IOException e) { e.printStackTrace(); }
SqlSessionFactory sf = new SqlSessionFactoryBuilder().build(is); SqlSession session = sf.openSession();
session.insert("insertStudent", student); session.commit(); session.close(); } }
|
- 测试类
1 2 3 4 5 6 7 8 9 10 11
| public class RunMain { public static void main(String[] args) { StudentDao dao=new StudentDaoImpl(); Student student=new Student(); student.setAge(23); student.setName("aa"); dao.insertStudent(student); } }
|