文章目录
一、Mybatis概述:( 一个持久层框架)
MyBatis 是一款优秀的持久层框架,它支持定制化 SQL、存储过程以及高级映射。MyBatis 避免了几乎所有的 JDBC 代码和手动设置参数以及获取结果集。MyBatis 可以使用简单的 XML 或注解来配置和映射原生信息,将接口和 Java 的 POJOs(Plain Ordinary Java Object,普通的 Java对象)映射成数据库中的记录。
二、第一个Mybatis程序编写
1. 创建项目,导入jar
2. 编写SqlMapConfig.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">
<!-- 默认JDBC事物管理取消了单条sql的自动提交 -->
<transactionManager type="JDBC" />
<dataSource type="POOLED">
<property name="driver" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/java"/>
<property name="username" value="root"/>
<property name="password" value=""/>
</dataSource>
</environment>
</environments>
<mappers>
<!-- <mapper resource="mapper/StudentMapper.xml"/>-->
<!-- 需要引入接口的方式进行调用 -->
<mapper class="com.zrgj.dao.IStudentDao"/>
</mappers>
</configuration>
3. 测试数据库是否连接正常
package test;
import com.zrgj.bean.Student;
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 org.junit.Test;
import java.io.IOException;
import java.io.InputStream;
public class test1 {
@Test
public void test1() throws IOException {
// 1. 先获取会话工厂构造器
SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder();
// 指定连接配置文件
InputStream is = Resources.getResourceAsStream("SqlMapConfig.xml");
// 2. 根据构造器,构造会话工厂
SqlSessionFactory factory = builder.build(is);
// 3. 开启连接会话
SqlSession session = factory.openSession();
System.out.println("连接: ..\n" + session);
}
4. 编写Mapper.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="StudentMapper">
<insert id="addStu" parameterType="com.zrgj.bean.Student">
insert into tb_student(name, sex, age) values(#{name}, #{sex}, #{age})
</insert>
<!-- 删除 -->
<delete id="delStu" parameterType="java.lang.Integer">
delete from tb_student where id=#{id}
</delete>
<!-- 修改 -->
<update id="updStu" parameterType="com.zrgj.bean.Student">
update tb_student set name=#{name}, sex=#{sex}, age=#{age} where id=#{id}
</update>
<!-- 单查询 -->
<select id="findStuById" parameterType="Integer" resultType="com.zrgj.bean.Student">
select *
from tb_student
where id=#{id}
</select>
</mapper>
5. 测试sql执行是否正常
package com.zrgj.controller;
import com.zrgj.bean.Student;
import com.zrgj.dao.IStudentDao;
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.InputStream;
import java.util.List;
import java.util.Scanner;
public class studentController {
public static void main(String[] args) throws IOException {
// 1. 先获取会话工厂构造器
SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder();
// 指定连接配置文件
InputStream is = Resources.getResourceAsStream("SqlMapConfig.xml");
// 2. 根据构造器,构造会话工厂
SqlSessionFactory factory = builder.build(is);
// 3. 开启连接会话
SqlSession session = factory.openSession();
// 先获取接口引用 - 类的类对象
IStudentDao studentDao = session.getMapper(IStudentDao.class);
System.out.println("0:退出;1:查询所有;2:添加;3:修改;4:通过ID查询;5:删除");
Scanner scanner = new Scanner(System.in);
while (true){
int re = scanner.nextInt();
switch (re){
case 0:
System.exit(0);
break;
case 1:
System.out.println("student表:");
List<Student> list = studentDao.findAll();
for (Student student:list){
System.out.println(student);
}
break;
case 2:
System.out.println("请输入要添加的姓名:");
String name = scanner.next();
System.out.println("请输入要添加的性别:");
String sex = scanner.next();
System.out.println("请输入要添加的年龄:");
int age = scanner.nextInt();
Student student = new Student(name, sex, age);
int i = studentDao.addStu(student);
if (i>0){
System.out.println("添加成功");
}else {
System.out.println("添加失败");
}
break;
case 3:
System.out.println("请输入要修改的姓名:");
String name1 = scanner.next();
System.out.println("请输入要修改的性别:");
String sex1 = scanner.next();
System.out.println("请输入要修改的年龄:");
int age1 = scanner.nextInt();
System.out.println("请输入要修改的id:");
int id = scanner.nextInt();
Student student1 = new Student(name1,sex1,age1,id);
int i1 = studentDao.updStu(student1);
if (i1>0){
System.out.println("修改成功");
}else {
System.out.println("修改失败");
}
break;
case 4:
System.out.println("请输入要查找的用户id:");
int stuId = scanner.nextInt();
Student byId = studentDao.findStuById(stuId);
System.out.println(byId);
break;
case 5:
System.out.println("请输入要删除的用户id:");
int delId = scanner.nextInt();
int i2 = studentDao.delStu(delId);
if (i2>0){
System.out.println("删除成功");
}else {
System.out.println("修改失败");
}
break;
}
}
}
}