第一步:首先需要新建一个数据库,然后建一个表在表里面可以随便插入几条数据,不插入也可以,等下直接用程序插入,今天就用下面这个表举例
第二步:创建一个Java工程;
第三步:创建一个实体类User,将数据库中的字段名get、set。
package com.zhiyuan.pojo;
public class User {
private int id;
private String name;
private String sex;
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;
}
}
- 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
第四步:写连接数据库的config.xml配置文件:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "mybatis-3-config.dtd" >
<configuration>
<typeAliases>
<typeAlias alias="User" type="com.zhiyuan.pojo.User"/>
</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://127.0.0.1:3306/mybatis" />
<property name="username" value="root" />
<property name="password" value="liuxi" />
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource="com/zhiyuan/pojo/User.xml" />
</mappers>
</configuration>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
第五步:配置对应实体类的user.XML文件,里面写MySQL增删改查语句:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "mybatis-3-mapper.dtd" >
<mapper namespace="com.zhiyuan.dao.Inter">
<!-- 插入数据,这里ID是自动递增的,所有不需要插入 -->
<insert id="getInsert" parameterType="User">
insert into nb(name,sex) values(#{name},#{sex})
</insert>
<!-- 查询表中所有的数据 -->
<select id="getUserList" resultType="com.zhiyuan.pojo.User">
select * from nb
</select>
<!-- 根据ID查询表数据 -->
<select id="getUser" parameterType="int" resultType="com.zhiyuan.pojo.User">
select * from nb where id=#{id}
</select>
<!-- 根据ID更新表数据 -->
<update id="getUpdate" parameterType="com.zhiyuan.pojo.User">
update nb set name=#{name},sex=#{sex} where id=#{id}
</update>
<!-- 根据ID删除表数据 -->
<delete id="getDelete" parameterType="int">
delete from nb where id=#{id}
</delete>
</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
第六步:写对应user.xml的接口类。
package com.zhiyuan.dao;
import java.util.List;
import com.zhiyuan.pojo.User;
public interface Inter {
public List<User> getUserList();
public User getUser(int id);
public void getInsert(User user);
public void getUpdate(User user);
public void getDelete(int id);
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
最后一步写测试类:
package com.zhiyuan.main;
import java.io.IOException;
import java.io.Reader;
import java.util.List;
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.zhiyuan.dao.Inter;
import com.zhiyuan.pojo.User;
public class Main {
static SqlSessionFactory ssf;
static SqlSession ss;
public static void main(String[] args) throws IOException {
Reader re = Resources.getResourceAsReader("conf.xml");
ssf = new SqlSessionFactoryBuilder().build(re);
ss = ssf.openSession();
insert();
// userList();
// user();
// update();
// delete();
}
//根据ID删除字段
private static void delete() {
ss = ssf.openSession();
Inter ie = ss.getMapper(Inter.class);
ie.getDelete(1);
ss.commit();
System.out.println("删除后:");
userList();
}
//根据ID更新表数据
private static void update() {
ss = ssf.openSession();
Inter ite = ss.getMapper(Inter.class);
User us = ite.getUser(3);
us.setName("徐深何");
us.setSex("不详");
ite.getUpdate(us);
ss.commit();
System.out.println("更新后:");
userList();
}
//条件查询
private static void user() {
ss = ssf.openSession();
Inter it = ss.getMapper(Inter.class);
User user = it.getUser(3);
if(user != null){
System.out.println("姓名:" + user.getName());
System.out.println( "性别:" + user.getSex());
}
}
//查询全部
private static void userList() {
ss = ssf.openSession();
Inter is = ss.getMapper(Inter.class);
print(is.getUserList());
}
private static void print(List<User> userList) {
for(User us : userList){
System.out.println("编号:" + us.getId());
System.out.println("姓名:" + us.getName());
System.out.println("性别:" + us.getSex());
}
}
//插入
private static void insert() {
ss = ssf.openSession();
Inter in = ss.getMapper(Inter.class);
User user = new User();
user.setName("CSDN");
user.setSex("????");
in.getInsert(user);
ss.commit();
System.out.println("插入成功");
userList();
}
}
- 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
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
最后输出结果为:
这里值得注意得到地方就是写配置文件时一定要细心,不然写到最后一运行一定会出现各种各样的错,找起来头痛。