首先,创建一个数据库my,并在数据库中插入一张表user,然后在user表中插入一行数据,代码如下:
create database my; use my;
create table user(
id int(10) auto_increment,
name varchar(64),
level varchar(256),
phone varchar(256),
primary key(id)
); insert into user(id,name,level,phone) values(1,'a','a','1234555666');
其次,创建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>
<typeAliases>
<typeAlias alias="User" type="com.zk.model.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://localhost:3306/my"/>
<property name="username" value="root"/>
<property name="password" value="123456"/>
</dataSource>
</environment>
</environments> <mappers>
<!-- // power by http://www.yiibai.com -->
<mapper resource="com/zk/model/UserMapper.xml" />
</mappers>
</configuration>
再次,创建一个IUser.java接口
package com.zk.dao; import java.util.List; import com.zk.model.User; public interface IUser {
//增加
public void addUser(User user);
//删除
public void deleteUser(int id);
//更新
public void updateUser(User user);
//删除
public User selectUser(int userid);
//查看所有使用者
public List<User> selectAllUser();
}
创建一个User.java实体类
package com.zk.model; public class User {
private int id;
private String name;
private String level;
private String phone;
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 getLevel() {
return level;
}
public void setLevel(String level) {
this.level = level;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
@Override
public String toString() {
return "User [id=" + id + ", name=" + name + ", level=" + level
+ ", phone=" + phone + "]";
}
}
接着,在配置UserMapper.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.zk.dao.IUser">
<insert id="addUser" parameterType="User">
Insert into user(id,name,level,phone) values(#{id},#{name},#{level},#{phone});
</insert> <delete id="deleteUser" parameterType="User">
delete from user where id=#{id};
</delete> <update id="updateUser" parameterType="User">
update user set name=#{name},level=#{level},phone=#{phone} where id=#{id};
</update> <select id="selectUser" parameterType="User" resultType="com.zk.model.User">
Select * from user where id=#{id};
</select> <select id="selectAllUser" resultType="com.zk.model.User">
Select * from user;
</select>
</mapper>
最后,构建一个Main.java,实现数据库增删改查的功能:
package MainApp; 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.zk.dao.IUser;
import com.zk.model.User; public class Main {
private static SqlSessionFactory sqlsessionfactory;
private static Reader reader; static{
try {
reader=Resources.getResourceAsReader("config/config.xml");
sqlsessionfactory=new SqlSessionFactoryBuilder().build(reader);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} public SqlSessionFactory getSqlSessionFactory()
{
return sqlsessionfactory;
} public static void main(String[]args)
{
//Insert();
//delete();
//update();
select(1);
} public static void Insert(){
//获取session连接
SqlSession session=sqlsessionfactory.openSession();
//获取mapper
IUser usermap=session.getMapper(IUser.class);
//执行插入
User user1=new User();
user1.setId(1);
user1.setLevel("a");
user1.setName("a");
user1.setPhone("19832321123");
usermap.addUser(user1);
//提交事务
//获取插入信息
session.commit();
selectAllUser();
} public static void delete(){
//获取session连接
SqlSession session=sqlsessionfactory.openSession();
//获取mapper
IUser usermap=session.getMapper(IUser.class);
//执行删除
usermap.deleteUser(1);
session.commit();
selectAllUser();
} public static void update(){
//获取session连接
SqlSession session=sqlsessionfactory.openSession();
//获取mapper
IUser usermap=session.getMapper(IUser.class);
selectAllUser();
User user = usermap.selectUser(1);
user.setName("c");
//执行删除
usermap.updateUser(user);
session.commit();
selectAllUser();
} public static void select(int id){
//获取session连接
SqlSession session=sqlsessionfactory.openSession();
//获取mapper
IUser usermap=session.getMapper(IUser.class);
//selectAllUser();
User user = usermap.selectUser(id);
//执行查询
System.out.println(user.toString());
session.commit();
//selectAllUser();
} private static void selectAllUser() {
// TODO Auto-generated method stub
SqlSession session=sqlsessionfactory.openSession();
IUser user=session.getMapper(IUser.class);
printUser(user.selectAllUser());
} private static void printUser(final List<User> users) {
// TODO Auto-generated method stub
int count = 0; for (User user : users) {
System.out.println("User Id: " + user.getId());
System.out.println("User Name: " + user.getName());
System.out.println("User Dept: " + user.getLevel());
System.out.println("User Website: " + user.getPhone());
}
}
}
这样就完成了数据库的增删改查。
程序结构如下图所示: