添加MyBatis的坐标
在pom.xml文件中加坐标
mysql mysql-connector-java 5.1.32 org.mybatis mybatis 3.4.6
添加user数据表创建user数据表对应的类编写映射文件UserMapper.xml
该文件内主要配置sql语句
<?xml version="1.0" encoding="UTF-8" ?> select * from user; insert into user values(#{id}, #{username}, #{password}) update user set username=#{username}, password=#{password} where id=#{id} delete from user where id=#{id}
配置核心文件SqlMapConfig.xml
主要配置数据源的环境,指明链接的数据库路径和账号信息
此外还得加载之前编写的映射文件UserMapper.xml,如果有其他文件的话也需要在核心文件中进行加载
<?xml version="1.0" encoding="UTF-8" ?>
jdbc.properties文件内容:
jdbc.driver=com.mysql.jdbc.Driverjdbc.url=jdbc:mysql://localhost:3306/testjdbc.username=rootjdbc.password=root
测试代码的编写
配置核心文件;获取会话工厂对象;获取会话对象;由会话对象执行sql操作
注:当数据库的信息需要更新时,sqlSession需要执行commit()方法提交事务,或者将openSession()的参数设置为true将自动提交事务。
package com.itheima.test;import com.itheima.domain.User;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.junit.Test;import java.io.IOException;import java.io.InputStream;import java.util.List;public class MyBatisTest { @Test //查询操作 public void test1() throws IOException { //获得核心配置文件 InputStream resourceAsStream = Resources.getResourceAsStream("sqlMapConfig.xml"); //获得session工厂对象 SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(resourceAsStream); //获得session会话对象 SqlSession sqlSession = sqlSessionFactory.openSession(); //执行在UserMapper映射文件中定义的sql操作;参数:namespace+id List userList = sqlSession.selectList("userMapper.findAll"); //打印输出 System.out.println(userList); //释放资源 sqlSession.close(); } @Test //插入操作 public void test2() throws IOException { //模拟user对象 User user = new User(); user.setUsername("sda"); user.setPassword("abc"); //获得核心配置文件 InputStream resourceAsStream = Resources.getResourceAsStream("sqlMapConfig.xml"); //获得session工厂对象 SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(resourceAsStream); //获得session会话对象 SqlSession sqlSession = sqlSessionFactory.openSession(); //执行在UserMapper映射文件中定义的sql操作;参数:namespace+id sqlSession.insert("userMapper.save", user); //mybatis执行更新操作,需要提交事务 sqlSession.commit(); //释放资源 sqlSession.close(); } @Test //修改操作 public void test3() throws IOException { //模拟user对象 User user = new User(); user.setId(7); user.setUsername("tom"); user.setPassword("123"); //获得核心配置文件 InputStream resourceAsStream = Resources.getResourceAsStream("sqlMapConfig.xml"); //获得session工厂对象 SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(resourceAsStream); //获得session会话对象 SqlSession sqlSession = sqlSessionFactory.openSession(); //执行在UserMapper映射文件中定义的sql操作;参数:namespace+id sqlSession.update("userMapper.update", user); //mybatis执行更新操作,需要提交事务 sqlSession.commit(); //释放资源 sqlSession.close(); } @Test //删除操作 public void test4() throws IOException { //模拟user对象 User user = new User(); user.setId(7); //获得核心配置文件 InputStream resourceAsStream = Resources.getResourceAsStream("sqlMapConfig.xml"); //获得session工厂对象 SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(resourceAsStream); //获得session会话对象 SqlSession sqlSession = sqlSessionFactory.openSession(); //执行在UserMapper映射文件中定义的sql操作;参数:namespace+id sqlSession.delete("userMapper.delete", user); //mybatis执行更新操作,需要提交事务 sqlSession.commit(); //释放资源 sqlSession.close(); }}