MyBatis框架
MyBatis 是一款优秀的持久层框架,它支持定制化 SQL、存储过程以及高级映射。MyBatis 避免了几乎所有的 JDBC 代码和手动设置参数以及获取结果集。MyBatis 可以使用简单的 XML 或注解来配置和映射原生信息,将接口和 Java 的 POJOs(Plain Old Java Objects,普通的 Java对象)映射成数据库中的记录。
MyBatis需要使用的jar包
mybatis-3.3.0.jar
mysql-connector-java-5.1.0-bin.jar
新建WEB项目
添加jar包
右键项目 --> Properties --> Add Library --> User Library --> Next --> User Libraries --> New --> Add External JARs -->选择jar路径 --> Apply and Close
新建包和配置文件
mybatis.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 resource="db.properties"/>
<!-- 实体类定义别名 -->
<typeAliases>
<typeAlias type="bean.User" alias="_User"/>
</typeAliases>
<environments default="cybatis">
<environment id="cybatis">
<!-- type="JDBC" 代表使用JDBC的提交和回滚来管理事务 -->
<transactionManager type="JDBC"/>
<dataSource type="POOLED">
<property name="driver" value="${driver}"/>
<property name="url" value="${url}"/>
<property name="username" value="${username}"/>
<property name="password" value="${password}"/>
</dataSource>
</environment>
</environments>
</configuration>
db.properties
driver=com.mysql.jdbc.Driver url=jdbc:mysql://localhost:3306/mybatis?useUnicode=true&characterEncoding=utf-8 username=root password=root
新建数据库
userMapper.xml
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE mapper PUBLIC "-//mybatis.org/DTD Mapper 3.0" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="mapper.UserMapper"> <!-- 根据id查询到一个User对象 --> <select id="getUser" parameterType="int" resultType="_User"> select * from user where uId=#{uId} </select> <!-- 创建用户信息 --> <insert id="insertUser" parameterType="_User"> insert into user(uName,uPwd,account) value(#{uName},#{uPwd},#{account}); </insert> <!-- 修改用户信息 --> <update id="updateUser" parameterType="_User"> update user set uName = #{uName},uPwd=#{uPwd},account=#{account} where uId = #{uId} </update> <!-- 删除用户信息 --> <delete id="deleteUser" parameterType="int"> delete from user where uId = #{uId}; </delete> <select id="getUserList" parameterType="int" resultType="_User"> select * from user </select> </mapper>
新建User实体类
package bean; public class User { private int uId; private String uName; private String uPwd; private double account; public int getuId() { return uId; } public void setuId(int uId) { this.uId = uId; } public String getuName() { return uName; } public void setuName(String uName) { this.uName = uName; } public String getuPwd() { return uPwd; } public void setuPwd(String uPwd) { this.uPwd = uPwd; } public double getAccount() { return account; } public void setAccount(double account) { this.account = account; } }
SQL映射接口
package mapper; import java.util.List; import org.apache.ibatis.annotations.Delete; import org.apache.ibatis.annotations.Insert; import org.apache.ibatis.annotations.Select; import org.apache.ibatis.annotations.Update; import bean.User; /** * @author Mr.Tong * SQL映射接口 */ public interface UserMapperI { /** 使用@Insert注解指明insertUser方法要执行的SQL * @param user 新增的用户对象 * @return 新增执行结果 */ @Insert("insert into user(uName,uPwd,account) value(#{uName},#{uPwd},#{account});") public int insertUser(User user); /** 使用@Delete注解指明delUser方法要执行的SQL * @param uId 删除的用户编号 * @return 删除执行结果 */ @Delete("delete from user where uId = #{uId}") public int delUser(int uId); /** 使用@Select注解指明getUser方法要执行的SQL * @param uId 查询的用户编号 * @return uId用户对象 */ @Select("select * from user where uId = #{uId}") public User getUser(int uId); /** 使用@Update注解指明updUser方法要执行的SQL * @param user User对象 * @return 执行修改结果 */ @Update("update user set uName=#{uName},uPwd=#{uPwd},account=#{account} where uId = #{uId}") public int updUser(User user); /** 使用@Select注解指明getAllUser方法要执行的SQL * @return 所有用户集合 */ @Select("select * from user") public List<User> getAllUser(); }
JUnit测试
import java.util.List; import org.apache.ibatis.session.SqlSession; import org.junit.jupiter.api.Test; import bean.User; import mapper.UserMapperI; import tool.MyBatisUtil; public class demo { @Test public void getUser() { SqlSession session = MyBatisUtil.getSqlSession(); UserMapperI userMapperI = session.getMapper(UserMapperI.class); User user = userMapperI.getUser(1); System.out.println(user.getuName()); } @Test public void addUser() { SqlSession session = MyBatisUtil.getSqlSession(true); UserMapperI userMapperI = session.getMapper(UserMapperI.class); User user = new User(); user.setuName("李四"); user.setuPwd("123123"); user.setAccount(123.93); int result = userMapperI.insertUser(user); System.out.println(result); } @Test public void updateUser() { SqlSession session = MyBatisUtil.getSqlSession(true); UserMapperI userMapperI = session.getMapper(UserMapperI.class); User user = new User(); user.setuId(3); user.setuName("王五"); user.setuPwd("123456"); user.setAccount(200.93); int result = userMapperI.updUser(user); System.out.println(result); } @Test public void deleteUser() { SqlSession session = MyBatisUtil.getSqlSession(true); UserMapperI userMapperI = session.getMapper(UserMapperI.class); int result = userMapperI.delUser(1); System.out.println(result); } @Test public void getUserList() { SqlSession session = MyBatisUtil.getSqlSession(true); UserMapperI userMapperI = session.getMapper(UserMapperI.class); List<User> uList = userMapperI.getAllUser(); System.out.println(uList.size()); } }
运行结果:
原文:https://www.cnblogs.com/qffs/p/9592237.html