【Mybatis】XML配置实现增删改查

①创建数据库和表,数据库为mytest,表为users

 CREATE DATABASE mytest;
USE mytest; DROP TABLE IF EXISTS users;
CREATE TABLE users(
id INT PRIMARY KEY auto_increment NOT NULL,
name VARCHAR(20),
age INT
)

②创建Java工程,导入相应的jar包

【Mybatis】XML配置实现增删改查

③创建配置文件conf.xml和数据库配置文件db.properties

 <?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> <properties resource="db.properties"></properties>
<environments default="development">
<environment id="development">
<transactionManager type="JDBC"></transactionManager>
<dataSource type="POOLED">
<property name="driver" value="${driver}"/>
<property name="url" value="${url}"/>
<property name="username" value="${name}"/>
<property name="password" value="${password}"/>
</dataSource>
</environment>
</environments>
</configuration>

conf.xml

 driver = com.mysql.jdbc.Driver
url = jdbc:mysql://localhost:3306/mytest
name = root
password = root

④创建实体类User

 package org.mybatis.domain;

 public class User {

     private int id;
private String name;
private int age; 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 getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return "User [id=" + id + ", name=" + name + ", age=" + age + "]";
} }

⑤创建sql映射文件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="org.mybatis.mapping.userMapper">
<!-- 根据id查询得到一个user对象 -->
<select id="getUser" parameterType="int" resultType="org.mybatis.domain.User">
select * from users where id=#{id}
</select> <select id="getAllUsers" resultType="org.mybatis.domain.User">
select * from users
</select> <insert id="addUser" parameterType="org.mybatis.domain.User">
insert into users(name, age) values (#{name}, #{age})
</insert> <update id="updateUser" parameterType="org.mybatis.domain.User">
update users set name=#{name}, age=#{age} where id=#{id}
</update> <delete id="deleteUser" parameterType="int">
delete from users where id=#{id}
</delete>
</mapper>

userMapper.xml

⑥向配置文件中注册 userMapper.xml 文件

 <mappers>
<mapper resource="org/mybatis/mapping/userMapper.xml"/>
</mappers> </configuration>

conf.xml

⑦测试类

 package org.mybatis.app;

 import java.io.InputStream;
import java.util.List; import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Before;
import org.junit.Test;
import org.mybatis.domain.User; public class TestXML { SqlSession session; @Before
public void beforeLoadXML(){
//加载 mybatis 配置文件
InputStream inputStream = TestXML.class.
getClassLoader().getResourceAsStream("conf.xml");
//构建sqlSession的工厂
SqlSessionFactory sqlSessionFactory =
new SqlSessionFactoryBuilder().build(inputStream);
//根据 sqlSessionFactory 产生 session
session = sqlSessionFactory.openSession();
} //插入数据
@Test
public void addOneUser() {
String statement = "org.mybatis.mapping.userMapper.addUser";
User user = new User();
user.setName("666");
user.setAge(66);
session.insert(statement, user);
session.commit();
session.close();
} //根据id查询user表中的数据
@Test
public void getOneUser(){
String statement = "org.mybatis.mapping.userMapper.getUser";
User user = session.selectOne(statement, 1);
System.out.println(user);
session.close();
} //查询所有数据
@Test
public void getAllUser() {
String statement = "org.mybatis.mapping.userMapper.getAllUsers";
List<User> listUser = session.selectList(statement);
System.out.println(listUser);
session.close();
} //更新数据
//session.update(statement, user);
//删除数据
//session.delete(statement, 1);
}

⑧结构图为:

【Mybatis】XML配置实现增删改查

上一篇:Webcast / 技术小视频制作方法——自己动手录制video轻松搞定


下一篇:pentaho专题系列之kettle篇--kettle源码编译