文章目录


目录结构

mybatis增删改查_xml

源码和数据库

链接:​​https://pan.baidu.com/s/1HqobDW7t4syxePWTu_cuBQ​

提取码:p5wv

SqlSessionFactoryUtils

package mybatis.utils;

import java.io.IOException;
import java.io.InputStream;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

public class SqlSessionFactoryUtils {

private static SqlSessionFactory sqlSessionFactory;

static {

try {

// 创建SqlSessionFactoryBuilder对象

SqlSessionFactoryBuilder ssfb = new SqlSessionFactoryBuilder();

// 创建核心配置文件的输入流

InputStream inputStream = Resources.getResourceAsStream("SqlMapConfig.xml");

// 通过输入流创建SqlSessionFactory对象

sqlSessionFactory = ssfb.build(inputStream);

} catch (IOException e) {

}

}

public static SqlSessionFactory getSqlSessionFactory() {
return sqlSessionFactory;
}
}

核心配置文件

<?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>
<!-- 和spring整合后 environments配置将废除 -->
<environments default="development">
<environment id="development">
<!-- 使用jdbc事务管理 -->
<transactionManager type="JDBC" />
<!-- 数据库连接池 -->
<dataSource type="POOLED">
<property name="driver" value="com.mysql.jdbc.Driver" />
<property name="url"
value="jdbc:mysql://localhost:3306/mybatis?characterEncoding=utf-8" />
<property name="username" value="root" />
<property name="password" value="root" />
</dataSource>
</environment>
</environments>

<mappers>
<mapper resource="mybatis/user.xml"/>
</mappers>
</configuration>

资源文件user.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="user"><!-- 指定命名空间 -->

<!-- 根据id查询 -->
<select id="getUserById" parameterType="int" resultType="pojo.User">
SELECT* FROM user where id=#{id}
</select>

<!-- 根据用户名模糊查询 -->
<select id="getUserByUserName" parameterType="string" resultType="pojo.User">
SELECT* FROM user where username like '%${value}%'
</select>

<!-- 插入用户数据 -->
<insert id="insertUser" parameterType="pojo.User">
INSERT INTO user VALUES(null, #{username}, #{birthday}, #{sex}, #{address} );
</insert>

<!-- 更新用户数据 -->
<update id="updateUser" parameterType="pojo.User">
UPDATE USER SET username = #{username} WHERE id = #{id}
</update>

<!-- 删除用户数据 -->
<delete id="deleteUser" parameterType="int">
DELETE FROM `user` WHERE `id` = #{id}
</delete>
</mapper>

user实体

package pojo;

import java.util.Date;

public class User {

private Integer id;
private String username;// 用户姓名
private String sex;// 性别
private Date birthday;// 生日
private String address;// 地址

public Integer getId() {
return id;
}

public void setId(Integer id) {
this.id = id;
}

public String getUsername() {
return username;
}

public void setUsername(String username) {
this.username = username;
}

public String getSex() {
return sex;
}

public void setSex(String sex) {
this.sex = sex;
}

public Date getBirthday() {
return birthday;
}

public void setBirthday(Date birthday) {
this.birthday = birthday;
}

public String getAddress() {
return address;
}

public void setAddress(String address) {
this.address = address;
}

@Override
public String toString() {
return "User [id=" + id + ", username=" + username + ", sex=" + sex + ", birthday=" + birthday + ", address="
+ address + "]";
}

}