MyBatis第一个程序
创建一个maven项目,并且在pom.xml导入myBatis和jdbc的jar包
<dependencies> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.5.2</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.47</version> </dependency> </dependencies>
在resource配置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.jdbc.Driver"/> <property name="url" value="jdbc:mysql://localhost:3306/mybatis?useSSL=true&useUnicode=true&characterEncoding=utf8"/> <property name="username" value="root"/> <property name="password" value="a1814995041"/> </dataSource> </environment> </environments> <mappers> <mapper resource="com/flsh/dao/userMapper.xml"/> </mappers> </configuration>
创建mybatis的工具类mybatisUtil.classpackage com.mybatis.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 mybatisUtil { private static SqlSessionFactory sqlSessionFactory; static{ //第一步创建sqlSessionFactory对象 try { String resource = "mybatis-config.xml";
//获取对应的配置文件 InputStream inputStream = Resources.getResourceAsStream(resource);
sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream); } catch (IOException e) { e.printStackTrace(); } } public static SqlSession getSqlSession(){ SqlSession sqlSession = sqlSessionFactory.openSession(); return sqlSession; } }
创建数据实体的文件夹pojo,再在pojo中新建User实体类
package com.mybatis.pojo; public class User { private Integer id; private String name; private String pwd; public User() { } public User(Integer id, String name, String pwd) { this.id = id; this.name = name; this.pwd = pwd; } @Override public String toString() { return "User{" + "id=" + id + ", name='" + name + '\'' + ", pwd='" + pwd + '\'' + '}'; } public Integer getId() { return id; } public void setId(Integer 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; } }
在java文件夹中创建dao的文件加下UserMapper.class的接口
package com.mybatis.mapper; import com.mybatis.pojo.User; import java.util.List; public interface UserMapper { List<User> selectUser(); void addUser(User user); void updateUser(User user); void deleteUser(int id); }
配对的UserMapper接口,在同一目录中创建UserMapper.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:接口名--> <mapper namespace="com.mybatis.mapper.UserMapper"> <!-- id:方法名 resultType:返回类型--> <select id="selectUser" resultType="com.mybatis.pojo.User"> select * from user </select> <update id="updateUser" parameterType="com.mybatis.pojo.User"> update user set name=#{name},pwd=#{pwd} where id=#{id} </update> <delete id="deleteUser" parameterType="int"> delete from user where id=#{id} </delete> <insert id="addUser" parameterType="com.mybatis.pojo.User"> insert into user (id,name,pwd) values (#{id},#{name},#{pwd}) </insert> </mapper>
在Test.java的测试文件夹,创建测试类UserTest.class
package com.mybatis.mapper; import com.mybatis.pojo.User; import com.mybatis.utils.mybatisUtil; import org.apache.ibatis.session.SqlSession; import org.junit.Test; import java.util.List; public class UserTest { @Test public void selectUser(){
//调用mybatis的工具类 SqlSession sqlSession = mybatisUtil.getSqlSession();
//mapper:映射器 通过sqlSession.getMapper获取UserMapper接口中的方法 UserMapper mapper = sqlSession.getMapper(UserMapper.class); List<User> users = mapper.selectUser(); for(User user : users){ System.out.println(user); } sqlSession.close(); } @Test public void addUser(){ SqlSession sqlSession = mybatisUtil.getSqlSession(); UserMapper mapper = sqlSession.getMapper(UserMapper.class); mapper.addUser(new User(4,"四郎","123456")); //提交事务 sqlSession.commit(); //关闭sql sqlSession.close(); } @Test public void updateUser(){
SqlSession sqlSession = mybatisUtil.getSqlSession(); UserMapper mapper = sqlSession.getMapper(UserMapper.class); mapper.updateUser(new User(4,"五郎","123456")); //提交事务 sqlSession.commit(); //关闭sql sqlSession.close(); } @Test public void deleteUser(){ SqlSession sqlSession = mybatisUtil.getSqlSession(); UserMapper mapper = sqlSession.getMapper(UserMapper.class); mapper.deleteUser(4); //提交事务 sqlSession.commit(); //关闭sql sqlSession.close(); } }