手把手教你编写MyBatis入门程序:轻松掌握核心技巧
导读:
MyBatis是一款流行的持久层框架,它能够帮助我们更便捷地与数据库进行交互。本文将以一个简单的示例为基础,手把手教你编写MyBatis入门程序,让你轻松掌握核心技巧。在编写过程中,我们将使用Java作为编程语言,以MySQL数据库作为示例。
一、环境准备
在开始之前,我们需要准备好以下环境:
二、创建Maven项目
<dependencies> <!-- MyBatis --> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.5.6</version> </dependency> <!-- MySQL --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>8.0.22</version> </dependency> </dependencies>
三、编写数据库配置文件
<configuration> <environments default="development"> <environment id="development"> <transactionManager type="JDBC"/> <dataSource type="POOLED"> <property name="driver" value="com.mysql.cj.jdbc.Driver"/> <property name="url" value="jdbc:mysql://localhost:3306/mybatis_demo?useUnicode=true&characterEncoding=utf8&serverTimezone=Asia/Shanghai"/> <property name="username" value="your_username"/> <property name="password" value="your_password"/> </dataSource> </environment> </environments> <mappers> <mapper resource="mapper/UserMapper.xml"/> </mappers> </configuration>
注意替换<property>
标签中的value
属性为你自己的数据库相关信息。
四、编写数据表对应的POJO类
public class User { private int id; private String name; private int age; // 省略getter和setter方法 }
二、编写Mapper接口和映射文件
public interface UserMapper { public User getUserById(int id); public List<User> getAllUsers(); public void addUser(User user); public void updateUser(User user); public void deleteUser(int id); }
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="com.example.mapper.UserMapper"> <select id="getUserById" parameterType="int" resultType="com.example.pojo.User"> SELECT * FROM user WHERE id = #{id} </select> <select id="getAllUsers" resultType="com.example.pojo.User"> SELECT * FROM user </select> <insert id="addUser" parameterType="com.example.pojo.User"> INSERT INTO user (name, age) VALUES (#{name}, #{age}) </insert> <update id="updateUser" parameterType="com.example.pojo.User"> UPDATE user SET name = #{name}, age = #{age} WHERE id = #{id} </update> <delete id="deleteUser" parameterType="int"> DELETE FROM user WHERE id = #{id} </delete> </mapper>
五、编写测试代码
public class App { public static void main(String[] args) { // 创建SqlSessionFactory对象 InputStream inputStream = Resources.getResourceAsStream("mybatis-config.xml"); SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream); // 获取SqlSession对象 SqlSession sqlSession = sqlSessionFactory.openSession(); try { // 获取UserMapper接口的实例 UserMapper userMapper = sqlSession.getMapper(UserMapper.class); // 测试getUserById方法 User user = userMapper.getUserById(1); System.out.println(user.getId() + " " + user.getName() + " " + user.getAge()); // 测试getAllUsers方法 List<User> userList = userMapper.getAllUsers(); for (User u : userList) { System.out.println(u.getId() + " " + u.getName() + " " + u.getAge()); } // 测试addUser方法 User newUser = new User(); newUser.setName("张三"); newUser.setAge(20); userMapper.addUser(newUser); sqlSession.commit(); // 测试updateUser方法 User updateUser = new User(); updateUser.setId(1); updateUser.setName("李四"); updateUser.setAge(25); userMapper.updateUser(updateUser); sqlSession.commit(); // 测试deleteUser方法 userMapper.deleteUser(1); sqlSession.commit(); } finally { sqlSession.close(); } } }
六、运行程序
结语:
通过本文的学习,相信你已经轻松掌握了MyBatis的核心技巧。希望你能在实际项目中运用MyBatis来提升开发效率。如果你想进一步学习MyBatis的高级用法,建议参考官方文档和相关书籍,不断深入学习和实践。祝你在MyBatis的学习之路上取得更大的成功!