创建第一个MyBatis实例(详细)
来源:互联网 发布:qq 微信 知乎 编辑:程序博客网 时间:2024/05/18 00:17
创建第一个MyBatis实例
(1),创建数据库中的表;
(2),创建项目并添加MyBatis框架所需的jar包;
(3),编写实体类;
(4),编写与实体类对应的映射接口(xxxMapper.java),以及映射配置文件(xxxMapper.xml);
(5),配置MyBatis框架的配置文件(mybatis-config.xml);
(6),编写MyBatis框架工具类;
(7),编写测试应用类并运行;
1,创建表: tb_student
create table TB_STUDENT( id int not null, `name`VARCHAR(50) not null, score int not null, PRIMARY KEY(id));
2,创建项目
添加所需的jar包到/WEB-INF/lib
3,编写实体类
编写与tb_student表对应的实体类Student,代码如下
package com.mybatis.pojos;public class Student { private int id; private String name; private int score; //必须要有无参的构造方法,不然根据StudentMapper.xml中的配置,在查询数据库时,将不能反射构造出Student实列 public Student(){ } //带参数的构造方法 public Student(int id,String name,int score){ this.id=id; this.name=name; this.score=score; } @Override public String toString(){ return "学号: "+id+"\n姓名: "+name+"\n成绩: "+score; } 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 getScore() { return score; } public void setScore(int score) { this.score = score; } }
4,关系映射
编写与Student实体类所对应的映射接口StudentMapper,该接口是用于数据访问操作的接口,代码如下
package com.mybatis.mapper;import com.mybatis.pojos.Student;public interface StudentMapper { public void insertStudent(Student student);//添加学生对象 public Student getStudent(String name); //查找学生对象}
编写对应的映射配置文件StudentMapper.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.mybatis.mapper.StudentMapper"> <insert id="insertStudent" parameterType="Student"> insert into tb_student(id,name,score)values(#{id},#{name},#{score}) </insert> <!-- 这里的id必须和StudentMapper接口中的接口方法名相同,不然运行时会报错 --> <select id="getStudent" resultType="Student" parameterType="java.lang.String" > select id,name,score from tb_student where name=#{name} </select></mapper>
5,配置mybatis-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> <settings> <setting name="cacheEnabled" value="false" /> <setting name="useGeneratedKeys" value="true" /> <setting name="defaultExecutorType" value="REUSE" /> </settings> <typeAliases> <typeAlias alias="Student" type="com.mybatis.pojos.Student" /> </typeAliases> <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/hibernateuser" /> <property name="username" value="root" /> <property name="password" value="root" /> </dataSource> </environment> </environments> <mappers> <mapper resource = "com/mybatis/mapper/StudentMapper.xml"></mapper> </mappers></configuration>
6,编写MyBatisUtil工具类
用于获取SqlSessionFactory对象,代码如下
package com.mybatis.util;import java.io.IOException;import java.io.Reader;import org.apache.ibatis.io.Resources;import org.apache.ibatis.session.SqlSessionFactory;import org.apache.ibatis.session.SqlSessionFactoryBuilder;public class MyBatisUtil { private final static SqlSessionFactory sqlSessionFactory; static{ String resource="mybatis-config.xml"; Reader reader=null; try{ //加载"mybatis-config.xml reader = Resources.getResourceAsReader(resource); }catch(IOException e){ System.out.println(e.getMessage()); } sqlSessionFactory =new SqlSessionFactoryBuilder().build(reader); } public static SqlSessionFactory getSqlSessionFactory(){ return sqlSessionFactory; }}
7,编写测试应用类
package com.mybatis;import org.apache.ibatis.session.SqlSession;import org.apache.ibatis.session.SqlSessionFactory;import com.mybatis.mapper.StudentMapper;import com.mybatis.pojos.Student;import com.mybatis.util.MyBatisUtil;public class MyBatisDemo { static SqlSessionFactory sqlSessionFactory=MyBatisUtil.getSqlSessionFactory(); public static void main(String[] args) { // //添加4个学生对象// addStudent(new Student(1,"it",89));// addStudent(new Student(2,"is",98));// addStudent(new Student(3,"so",99));// addStudent(new Student(4,"easy",99)); //根据姓名查找 Student student=getStudent("niu"); System.out.println(student.toString()); } //添加学生信息 public static void addStudent(Student student){ //打开SqlSession SqlSession sqlSession = sqlSessionFactory.openSession(); try{ //获取Mapper对象 StudentMapper studentMapper=sqlSession.getMapper(StudentMapper.class); //插入操作 studentMapper.insertStudent(student); //提交SqlSession sqlSession.commit(); System.out.println("添加成功"); }finally{ //关闭sqlSession sqlSession.close(); } } //根据姓名查找学生信息 public static Student getStudent(String name){ Student student=null; //打开SqlSession SqlSession sqlSession = sqlSessionFactory.openSession(); try{ //获取Mapper对象 StudentMapper studentMapper=sqlSession.getMapper(StudentMapper.class); //查找操作 student=studentMapper.getStudent(name); }finally{ //关闭sqlSession sqlSession.close(); } return student; }}
项目文件目录如下:
阅读全文
0 0
- 创建第一个MyBatis实例(详细)
- mybatis创建第一个实例的笔记(1)
- mybatis新手教程-第一个mybatis实例
- IntelliJ IDEA 创建第一个Mybatis工程
- Mybatis(二)-创建第一个应用
- 创建第一个Spring MVC(详细)
- 使用HighCharts创建第一个图表实例
- cordova创建第一个iOS程序实例
- 使用MyEclipse创建第一个Hibernate实例
- 创建第一个node,express,ejs网站详细介绍
- [推荐]图文详细,创建第一个AndroidStudio项目
- 第一个mybatis例子
- mybatis第一个helloworld
- 第一个MyBatis程序
- Mybatis 第一个Demo
- mybatis第一个问题
- 第一个Mybatis
- 我的第一个Android实例之创建AVD总结
- 组播和广播编程
- android 电池与电量
- 判断一个字符串是否能够通过添加一个字符变成回文序列的方法
- weex sdk集成到Android工程
- OC : 新的 NSFormatter 子类
- 创建第一个MyBatis实例(详细)
- HihoCoder1388(Periodic Signal)-fft(快速傅里叶变换)
- FLV视频合并-JAVA代码
- 将HTMLCollection/NodeList/伪数组转换成数组
- Got fatal error 1236 from master when reading data from binary log: 'A slave with the same server_uu
- 掀开<mvc:annotation-driven/>的面纱
- WEB应用中的SESSION详解
- 常用脚本(持续更新)
- node.js实现用户登录注册简单示例