Mybatis初学习
2021/9/6 6:09:10
本文主要是介绍Mybatis初学习,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
Mybatis初学习
1.导入maven mybatis包依赖
<!-- https://mvnrepository.com/artifact/org.mybatis/mybatis --> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.5.6</version> </dependency>
2.在resources创建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> <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?serverTimezone=UTC&useUnicode=true&characterEncoding=utf8&useSSL=true"/> <property name="username" value="root"/> <property name="password" value="123456"/> </dataSource> </environment> </environments> </configuration>
在xml文件中,&符号要用&代替
3.创建mybatis工具类,获取SqlSession
public class MybatisUtils { private static SqlSessionFactory sqlSessionFactory = null; static{ String resource = "mybatis-config.xml"; InputStream inputStream = null; try { inputStream = Resources.getResourceAsStream(resource); } catch (IOException e) { e.printStackTrace(); } sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream); } public static SqlSession getSqlSession(){ return sqlSessionFactory.openSession(); } }
4.创建user实体类
public class User { private int id; private String name; private String pwd; public User() { } public User(int id, String name, String pwd) { this.id = id; this.name = name; this.pwd = pwd; } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getPwd() { return pwd; } public void setPwd(String pwd) { this.pwd = pwd; } @Override public String toString() { return "User{" + "id=" + id + ", name='" + name + '\'' + ", pwd='" + pwd + '\'' + '}'; } }
5.创建UserMapper接口,即是原来的dao接口
public interface UserMapper { //查询所有用户 List<User> getUserList(); //查询指定用户 User getUser(int id); //增加用户 int insertUser(User user); //修改用户 int updateUser(User user); //删除用户 int deleteUser(int id); }
6.创建UserMapper对应的xml配置文件,即是原来UserDao接口的实现类
<?xml version="1.0" encoding="GBK" ?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <!--每一个mapper都要配置--> <mapper namespace="com.sise.dao.UserMapper"> <select id="getUserList" resultType="com.sise.entity.User"> select * from `user` </select> <select id="getUser" resultType="com.sise.entity.User"> select * from `user` where `id`=#{id} </select> <insert id="insertUser" parameterType="com.sise.entity.User"> insert into `user` value (#{id},#{name},#{pwd}) </insert> <update id="updateUser" parameterType="com.sise.entity.User"> update `user` set `name`=#{name},`pwd`=#{pwd} where `id`=#{id} </update> <delete id="deleteUser" parameterType="int"> delete from `user` where `id`=#{id} </delete> </mapper>
注意点:有中文注释可能会报错,要设置encoding=“GBK”
7.在mybatis-config.xml添加mapper配置
<mappers> <mapper resource="com/sise/dao/UserMapper.xml"/> </mappers>
resource的路径要用/,如果用com.sise.dao.UserMapper.xml会报错找不到文件
8.编写测试类
public class UserMapperTest { @Test public void getUserList(){ SqlSession sqlSession = MybatisUtils.getSqlSession(); UserMapper userMapper = sqlSession.getMapper(UserMapper.class); List<User> userList = userMapper.getUserList(); for (User user : userList) { System.out.println(user); } //关闭资源 sqlSession.close(); } @Test public void getUser(){ SqlSession sqlSession = MybatisUtils.getSqlSession(); UserMapper mapper = sqlSession.getMapper(UserMapper.class); User user = mapper.getUser(1); System.out.println(user); sqlSession.close(); } @Test public void insertUser(){ SqlSession sqlSession = MybatisUtils.getSqlSession(); UserMapper mapper = sqlSession.getMapper(UserMapper.class); mapper.insertUser(new User(4,"赵六","123456")); sqlSession.commit(); sqlSession.close(); } @Test public void updateUser(){ SqlSession sqlSession = MybatisUtils.getSqlSession(); UserMapper mapper = sqlSession.getMapper(UserMapper.class); mapper.updateUser(new User(4,"秦八","123456")); sqlSession.commit(); sqlSession.close(); } @Test public void deleteUser(){ SqlSession sqlSession = MybatisUtils.getSqlSession(); UserMapper mapper = sqlSession.getMapper(UserMapper.class); mapper.deleteUser(4); sqlSession.commit(); sqlSession.close(); } }
这篇关于Mybatis初学习的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-11-29RocketMQ底层原理资料详解:新手入门教程
- 2024-11-29RocketMQ源码资料解析与入门教程
- 2024-11-29[开源]6.1K star!这款电视直播源神器真的太赞啦!
- 2024-11-29HTTP压缩入门教程:轻松提升网页加载速度
- 2024-11-29JWT开发入门指南
- 2024-11-28知识管理革命:文档软件的新玩法了解一下!
- 2024-11-28低代码应用课程:新手入门全攻略
- 2024-11-28哪些办公软件适合团队协作,且能够清晰记录每个阶段的工作进展?
- 2024-11-28全栈低代码开发课程:零基础入门到初级实战
- 2024-11-28拖动排序课程:轻松掌握课程拖动排序功能