Mybatis配置数据库连接

首先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/ssms?useSSL=true&amp;useUnicode=true&amp;characterEncoding=UTF-8"/> <property name="username" value="root"/> <property name="password" value="123456"/> </dataSource> </environment> </environments> <mappers> <mapper resource="cn/zmxy/dao/UsersMapper.xml"/> </mappers> </configuration>

pojo实现类层的Java类(对应数据库表),比如Users表

public class Users { private int id; private String name; private String pwd; public Users(){} }

dao层的映射xml和Interface接口

UsersMapper.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"> <!--namespace 命名空间 保定一个对应的dao/mapper接口--> <mapper namespace="cn.zmxy.dao.UsersMapper"> <select id="getUserList" resultType="cn.zmxy.pojo.Users"> SELECT * FROM ssms.users </select> <select id="getUserById" resultType="cn.zmxy.pojo.Users" parameterType="int"> SELECT * FROM ssms.users WHERE id = #{id} </select> <insert id="addUser" parameterType="cn.zmxy.pojo.Users"> INSERT INTO ssms.users (id, name, pwd) VALUES (#{id}, #{name}, #{pwd}) </insert> <update id="updateUser" parameterType="cn.zmxy.pojo.Users"> UPDATE ssms.users SET name = #{name}, pwd = #{pwd} WHERE id = #{id} </update> <delete id="deleteUser" parameterType="int"> DELETE FROM ssms.users WHERE id = #{id} </delete> </mapper>

UsersMapper.java

package cn.zmxy.dao; import cn.zmxy.pojo.Users; import java.util.List; public interface UsersMapper { // 查询全部用户 public List<Users> getUserList(); // 根据ID查询用户 public Users getUserById(int id); // 添加用户 public int addUser(Users user); // 修改用户 public int updateUser(Users user); // 删除用户 public int deleteUser(int id); }

写一个utils类,获取SqlSession

MyBatisUtils.java

package cn.zmxy.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 java.io.IOException; import java.io.InputStream; public class MyBatisUtils { private static SqlSessionFactory sqlSessionFactory; static { try { String resource = "mybatis-config.xml"; InputStream is = Resources.getResourceAsStream(resource); sqlSessionFactory = new SqlSessionFactoryBuilder().build(is); } catch (IOException e) { e.printStackTrace(); } } public static SqlSession getSqlSession() { return sqlSessionFactory.openSession(); }}

最后就可以调用了,写一个test类

UserMapperTest.java

package cn.zmxy.dao; import cn.zmxy.pojo.Users; import cn.zmxy.utils.MyBatisUtils; import org.apache.ibatis.session.SqlSession; import org.junit.Test; import java.util.List; public class UserDaoTest { @Test public void test() { // 第一步,获取SqlSession对象 SqlSession sqlSession = MyBatisUtils.getSqlSession(); // 第二步,执行SQL UsersMapper usersMapper = sqlSession.getMapper(UsersMapper.class); List<Users> userList = usersMapper.getUserList(); // 打印users for (Users users : userList) { System.out.println(users); } // 关闭SqlSession sqlSession.close(); } @Test public void getUserById() { // 第一步,获取SqlSession对象 SqlSession sqlSession = MyBatisUtils.getSqlSession(); UsersMapper mapper = sqlSession.getMapper(UsersMapper.class); Users user = mapper.getUserById(1); System.out.println(user); // 关闭SqlSession sqlSession.close(); } @Test public void addUser() { // 第一步,获取SqlSession对象 SqlSession sqlSession = MyBatisUtils.getSqlSession(); UsersMapper mapper = sqlSession.getMapper(UsersMapper.class); int num = mapper.addUser(new Users(0, "露露", "1234")); System.out.println("受影响行数:" + num); sqlSession.commit(); // 关闭SqlSession sqlSession.close(); } @Test public void updateUser() { // 第一步,获取SqlSession对象 SqlSession sqlSession = MyBatisUtils.getSqlSession(); UsersMapper mapper = sqlSession.getMapper(UsersMapper.class); int num = mapper.updateUser(new Users(3, "露露", "1234")); System.out.println("受影响行数:" + num); sqlSession.commit(); // 关闭SqlSession sqlSession.close(); } @Test public void deleteUser() { // 第一步,获取SqlSession对象 SqlSession sqlSession = MyBatisUtils.getSqlSession(); UsersMapper mapper = sqlSession.getMapper(UsersMapper.class); int num = mapper.deleteUser(11); System.out.println("受影响行数:" + num); sqlSession.commit(); // 关闭SqlSession sqlSession.close(); } }

__EOF__

本文作者织梦行云
本文链接https://www.cnblogs.com/cxfs/p/14763764.html
关于博主:评论和私信会在第一时间回复。或者直接私信我。
版权声明:本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!
声援博主:如果您觉得文章对您有帮助,可以点击文章右下角推荐一下。您的鼓励是博主的最大动力!
posted @   遗失的美好灬  阅读(387)  评论(0编辑  收藏  举报
编辑推荐:
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
· 一个奇形怪状的面试题:Bean中的CHM要不要加volatile?
· [.NET]调用本地 Deepseek 模型
阅读排行:
· 全网最简单!3分钟用满血DeepSeek R1开发一款AI智能客服,零代码轻松接入微信、公众号、小程
· .NET 10 首个预览版发布,跨平台开发与性能全面提升
· 《HelloGitHub》第 107 期
· 全程使用 AI 从 0 到 1 写了个小工具
· 从文本到图像:SSE 如何助力 AI 内容实时呈现?(Typescript篇)
点击右上角即可分享
微信分享提示