今天分享mybatis的简单crud
首先进行项目环境的搭建:
如下图所示
创建配置文件:
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>
<!--引入外部的properties文件,必须放在最前面-->
<properties resource="jdbc.properties"></properties>
<!-- 设置别名-->
<typeAliases>
<package name="com.jian.pojo"/>
</typeAliases>
<!--配置环境-->
<environments default="development">
<environment id="development">
<transactionManager type="JDBC"/>
<dataSource type="POOLED">
<property name="driver" value="${jdbc_driver}"/>
<property name="url" value="${jdbc_url}"/>
<property name="username" value="${jdbc_username}"/>
<property name="password" value="${jdbc_password}"/>
</dataSource>
</environment>
</environments>
<!--告诉mybatis映射文件的位置-->
<mappers>
<mapper resource="mapper/Usermapper.xml"/>
</mappers>
</configuration>
jdbc.properties数据库连接四要素
为了避免操作数据库时出现中文乱码,一定注意上图标注
#5.1.47
jdbc_driver=com.mysql.jdbc.Driver
jdbc_url=jdbc:mysql:///onlinedb?userUnicode=true&characterEncoding=utf8
jdbc_username=root
jdbc_password=123456
log4j.properties日志配置文件
#5.1.47
jdbc_driver=com.mysql.jdbc.Driver
jdbc_url=jdbc:mysql:///onlinedb?userUnicode=true&characterEncoding=utf8
jdbc_username=root
jdbc_password=123456
实体类的创建,使用@Data注解注意下载lombok插件
package com.jian.pojo;
import lombok.Data;
import org.apache.ibatis.type.Alias;
/**
* @description:
* @author: ajian
* @package: com.jian.pojo
* @date:2020/10/11 16:43
*/
@Data
@Alias("user")
public class User {
private Integer uID;
private String uName;
private String uSex;
}
UserDao层接口:
package com.jian.dao;
import com.jian.pojo.User;
import java.util.List;
import java.util.Map;
/**
* @description:userDao接口类
* @author: ajian
* @package: com.jian.dao
* @date:2020/10/11 14:35
*/
public interface UserDao {
/*查询全部*/
List<User> findAll();
/*根据ID查询*/
User findByID(Integer id);
/*模糊查询*/
List<User> findByLikeName(String uName);
/*分页查询*/
List<User> findBySplit(Map map);
/*模糊+分页查询*/
List<User> findBySplitLikeName(Map map);
/*添加*/
int add(User user);
/*修改*/
int update(User user);
/*删除*/
int delete(int id);
}
MybatisUtils类:工具类,读取mybatis配置文件的工具类–>拿到SQLSession
package com.jian.utils;
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 org.apache.log4j.lf5.util.Resource;
import java.io.IOException;
import java.io.Reader;
/**
* @description:读取mybatis配置文件的工具类-->拿到SQLSession
* @author: ajian
* @package: com.jian.utils
* @date:2020/10/11 14:40
*/
public class MybatisUtils {
private static SqlSessionFactory sqlSessionFactory = null;
static {
/*读取配置文件*/
Reader reader = null;
/*通过sqlSessionFactory工厂,得到一个会话sqlsession*/
SqlSession sqlSession = null;
try {
reader = Resources.getResourceAsReader("mybatis-config.xml");
} catch (IOException e) {
e.printStackTrace();
}
sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
}
public static SqlSession getSqlSession(){
return sqlSessionFactory.openSession();
}
}
Usermapper.xml配置文件:主要操作数据库的配置文件
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.jian.dao.UserDao">
<!--查询全部-->
<select id="findAll" parameterType="user" resultType="user">
select * from user
</select>
<!--根据ID查询-->
<select id="findByID" parameterType="integer" resultType="user">
select * from user where uID = #{id}
</select>
<!--模糊查询-->
<select id="findByLikeName" parameterType="String" resultType="user">
select * from user where uName like concat('%',#{value},'%')
-- select * from user where uName like '${likeName}%'
</select>
<!--分页查询-->
<select id="findBySplit" parameterType="map" resultType="user">
SELECT * FROM user LIMIT #{start},#{pageSize}
</select>
<!-- /*模糊+分页查询*/-->
<select id="findBySplitLikeName" parameterType="map" resultType="user">
select * from user
where uName like concat('%',#{likeName},'%')
LIMIT #{start},#{pageSize}
</select>
<!-- 添加字段-->
<insert id="add" parameterType="user">
insert into user(uName, uSex) values(#{uName},#{uSex})
</insert>
<!-- 修改字段-->
<update id="update" parameterType="user">
update user
set uName=#{uName},uSex=#{uSex}
where uID=#{uID}
</update>
<!--删除字段-->
<delete id="delete" parameterType="int">
delete from user
where uID=#{id}
</delete>
</mapper>
注意:
1.如果想要有映射连接的绿色箭头,请下载插件mybatis插件
2.图中标注的注意事项一定注意,有些是笔者亲身踩坑
3.一定注意配置文件中的语句是否与mysql表中的字段相同
以上就是今天的mybatis_crud的分享!