Mybatis入门
步骤
1、编写User数据表
2、编写User实体类
package com.example.MyBatis_Learn1;
import org.apache.ibatis.annotations.Param;
public class User {
private int id;
private String userName;
private String userPwd;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getUserPwd() {
return userPwd;
}
public void setUserPwd(String userPwd) {
this.userPwd = userPwd;
}
@Override
public String toString() {
return "User: " +
"id=" + id +
", userName='" + userName + '\'' +
", userPwd='" + userPwd + '\'' ;
}
}
3、编写用户映射文件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">
<mapper namespace="userMapper">
<select id="findAll" resultType="com.example.MyBatis_Learn1.User">
select * from user
</select>
</mapper>
4、编写核心文件SqlMapConfig.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">
<!--数据库驼峰适配实体类-->
<settings>
<setting name="mapUnderscoreToCamelCase" value="true"/>
</settings>
<mapper namespace="userMapper">
<resultMap id="user" type="com.example.MyBatis_Learn1.User">
<id property="id" column="id"/>
<result property="userName" column="name"/>
<result property="userPwd" column="password"/>
</resultMap>
<delete id="delete" parameterType="java.lang.Integer">
delete from user where id=#{id}
</delete>
<insert id="save" parameterType="com.example.MyBatis_Learn1.User">
insert into user value (#{id},#{userName},#{userPwd})
</insert>
<update id="updata" parameterType="com.example.MyBatis_Learn1.User">
update user set user_name = #{userName},user_pwd=#{userPwd} where id=#{id}
</update>
<select id="findAll" resultType="com.example.MyBatis_Learn1.User">
select * from user
</select>
</mapper>
5、编写测试类
package com.example.test;
import com.example.MyBatis_Learn1.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 java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.List;
public class Test1 {
public void delete(int id) throws IOException {
//获得核心配置文件
InputStream resourceAsStream = Resources.getResourceAsStream("confige/sqlMapperconfig.xml");
//获得session工厂对象
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(resourceAsStream);
//获得会话对象
SqlSession sqlSession = sqlSessionFactory.openSession();
sqlSession.delete("userMapper.delete",id);
sqlSession.commit();
sqlSession.close();
}
public void updata(User user) throws IOException {
//获得核心配置文件
InputStream resourceAsStream = Resources.getResourceAsStream("confige/sqlMapperconfig.xml");
//获得session工厂对象
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(resourceAsStream);
//获得会话对象
SqlSession sqlSession = sqlSessionFactory.openSession();
sqlSession.update("userMapper.updata",user);
sqlSession.commit();
sqlSession.close();
}
public void select() throws IOException {
//获得核心配置文件
InputStream resourceAsStream = Resources.getResourceAsStream("confige/sqlMapperconfig.xml");
//获得session工厂对象
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(resourceAsStream);
//获得会话对象
SqlSession sqlSession = sqlSessionFactory.openSession();
List<User> list = sqlSession.selectList("userMapper.findAll");
System.out.println(list);
sqlSession.close();
}
public void insert(User user) throws IOException {
//获得核心配置文件
InputStream resourceAsStream = Resources.getResourceAsStream("confige/sqlMapperconfig.xml");
//获得session工厂对象
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(resourceAsStream);
//获得会话对象
SqlSession sqlSession = sqlSessionFactory.openSession();
sqlSession.insert("userMapper.save",user);
sqlSession.commit();
sqlSession.close();
}
public static void main(String[] args) throws IOException {
}
}
SqlMapConfig.xml
UserMapper.xml
测试类