<!-- https://mvnrepository.com/artifact/org.mybatis/mybatis --> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.5.6</version> </dependency>
<?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文件中,&符号要用&代替
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(); } }
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 + '\'' + '}'; } }
public interface UserMapper { //查询所有用户 List<User> getUserList(); //查询指定用户 User getUser(int id); //增加用户 int insertUser(User user); //修改用户 int updateUser(User user); //删除用户 int deleteUser(int id); }
<?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”
<mappers> <mapper resource="com/sise/dao/UserMapper.xml"/> </mappers>
resource的路径要用/,如果用com.sise.dao.UserMapper.xml会报错找不到文件
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(); } }