mybatis框架对表实现增删改查 源代码
时间: 2023-11-18 13:55:53 浏览: 84
以下是一个简单的使用 MyBatis 框架进行增删改查的示例:
1. 配置文件
在 MyBatis 配置文件中,需要配置数据库连接信息、SQL 映射文件(Mapper)的位置等,例如:
```
<?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">
<transactionManager type="JDBC" />
<dataSource type="POOLED">
<property name="driver" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost:3306/test" />
<property name="username" value="root" />
<property name="password" value="123456" />
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource="com/example/mappers/UserMapper.xml" />
</mappers>
</configuration>
```
2. 实体类
定义一个实体类,例如:
```
public class User {
private Integer id;
private String name;
private Integer age;
// 省略 getter 和 setter 方法
}
```
3. SQL 映射文件
在 SQL 映射文件中定义 SQL 语句和对应的参数映射,例如:
```
<?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.example.mappers.UserMapper">
<resultMap id="userMap" type="com.example.entities.User">
<id column="id" property="id" />
<result column="name" property="name" />
<result column="age" property="age" />
</resultMap>
<select id="getUserById" resultMap="userMap">
SELECT * FROM users WHERE id = #{id}
</select>
<insert id="addUser" parameterType="com.example.entities.User">
INSERT INTO users (name, age) VALUES (#{name}, #{age})
</insert>
<update id="updateUser" parameterType="com.example.entities.User">
UPDATE users SET name = #{name}, age = #{age} WHERE id = #{id}
</update>
<delete id="deleteUserById">
DELETE FROM users WHERE id = #{id}
</delete>
</mapper>
```
4. Dao 接口
定义一个 Dao 接口,其中的方法名要与 SQL 映射文件中的 id 名称一致,例如:
```
public interface UserDao {
User getUserById(Integer id);
void addUser(User user);
void updateUser(User user);
void deleteUserById(Integer id);
}
```
5. Dao 实现类
实现 Dao 接口,使用 MyBatis 提供的 SqlSession 和 Mapper 接口来实现增删改查,例如:
```
public class UserDaoImpl implements UserDao {
private SqlSessionFactory sqlSessionFactory;
public UserDaoImpl(SqlSessionFactory sqlSessionFactory) {
this.sqlSessionFactory = sqlSessionFactory;
}
@Override
public User getUserById(Integer id) {
try (SqlSession session = sqlSessionFactory.openSession()) {
return session.selectOne("com.example.mappers.UserMapper.getUserById", id);
}
}
@Override
public void addUser(User user) {
try (SqlSession session = sqlSessionFactory.openSession()) {
session.insert("com.example.mappers.UserMapper.addUser", user);
session.commit();
}
}
@Override
public void updateUser(User user) {
try (SqlSession session = sqlSessionFactory.openSession()) {
session.update("com.example.mappers.UserMapper.updateUser", user);
session.commit();
}
}
@Override
public void deleteUserById(Integer id) {
try (SqlSession session = sqlSessionFactory.openSession()) {
session.delete("com.example.mappers.UserMapper.deleteUserById", id);
session.commit();
}
}
}
```
6. 使用 Dao 接口
使用 Dao 接口中定义的方法来操作数据,例如:
```
public static void main(String[] args) {
try (InputStream inputStream = Resources.getResourceAsStream("mybatis-config.xml")) {
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
UserDao userDao = new UserDaoImpl(sqlSessionFactory);
// 查询用户
User user = userDao.getUserById(1);
System.out.println(user.getName() + " " + user.getAge());
// 添加用户
User newUser = new User();
newUser.setName("Tom");
newUser.setAge(20);
userDao.addUser(newUser);
// 更新用户
User updateUser = new User();
updateUser.setId(1);
updateUser.setName("Jerry");
updateUser.setAge(25);
userDao.updateUser(updateUser);
// 删除用户
userDao.deleteUserById(2);
} catch (IOException e) {
e.printStackTrace();
}
}
```
阅读全文