Mybatis的增删查改
1、插入数据操作
<?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">
<!--插入操作-->
<insert id="save" parameterType="com.domain.User">
insert into user values(#{id},#{username},#{password})
</insert>
<!--查询操作-->
<select id="findAll" resultType="com.domain.User">
select * from user
</select>
</mapper>
测试
package com.test;
import com.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 test02() throws IOException {
//模拟user对象
User user=new User();
user.setId(7);
user.setUsername("tom");
user.setPassword("abc");
//获得核心配置文件
InputStream resourceAsStream = Resources.getResourceAsStream("sqlMapConfig.xml");
//获得session工厂对象
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(resourceAsStream);
//获得session回话对象
SqlSession sqlSession = sqlSessionFactory.openSession();
//执行操作 参数:namespace+id
sqlSession.insert("userMapper.save",user);
//mybatis执行更新操作 提交事务
sqlSession.commit();
//释放资源
sqlSession.close();
}
}
2、插入操作注意事项
2、修改数据操作
package com.test;
import com.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 test03() throws IOException {
//模拟user对象
User user=new User();
user.setId(3);
user.setUsername("lucy");
user.setPassword("456");
//获得核心配置文件
InputStream resourceAsStream = Resources.getResourceAsStream("sqlMapConfig.xml");
//获得session工厂对象
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(resourceAsStream);
//获得session回话对象
SqlSession sqlSession = sqlSessionFactory.openSession();
//执行操作 参数:namespace+id
sqlSession.update("userMapper.update",user);
//mybatis执行更新操作 提交事务
sqlSession.commit();
//释放资源
sqlSession.close();
}
}
<?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">
<!--修改操作-->
<update id="update" parameterType="com.domain.User">
update user set username=#{username},password=#{password} where id=#{id}
</update>
<!--插入操作-->
<insert id="save" parameterType="com.domain.User">
insert into user values(#{id},#{username},#{password})
</insert>
<!--查询操作-->
<select id="findAll" resultType="com.domain.User">
select * from user
</select>
</mapper>
2.1、注意问题
3、删除操作
<?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">
<!--删除操作-->
<delete id="delete" parameterType="java.lang.Integer">
delete from where id=#{id}
</delete>
<!--修改操作-->
<update id="update" parameterType="com.domain.User">
update user set username=#{username},password=#{password} where id=#{id}
</update>
<!--插入操作-->
<insert id="save" parameterType="com.domain.User">
insert into user values(#{id},#{username},#{password})
</insert>
<!--查询操作-->
<select id="findAll" resultType="com.domain.User">
select * from user
</select>
</mapper>
package com.test;
import com.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 test04() throws IOException {
//获得核心配置文件
InputStream resourceAsStream = Resources.getResourceAsStream("sqlMapConfig.xml");
//获得session工厂对象
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(resourceAsStream);
//获得session回话对象
SqlSession sqlSession = sqlSessionFactory.openSession();
//执行操作 参数:namespace+id
sqlSession.delete("userMapper.delete",7);
//mybatis执行更新操作 提交事务
sqlSession.commit();
//释放资源
sqlSession.close();
}
}
3.1、删除操作