标签:oca unset racket correct efi 测试 告诉 出现 rom
<mapper namespace="dulk.learn.mybatis.dao.GirlDao">
<select id="findById" parameterType="long" resultType="dulk.learn.mybatis.pojo.Girl">
SELECT * FROM girl WHERE id = #{id}
</select>
</mapper>
<mapper namespace="dulk.learn.mybatis.dao.GirlDao">
<select id="findById" parameterType="long" resultType="dulk.learn.mybatis.pojo.Girl">
SELECT * FROM girl WHERE id = #{id}
</select>
</mapper>
String findById = "SELECT * FROM girl WHERE id = ?";
PreparedStatement ps = conn.prepareStatement(findById);
ps.setLong(1, id);
String findById = "SELECT * FROM girl WHERE id = ?";
PreparedStatement ps = conn.prepareStatement(findById);
ps.setLong(1, id);
<insert id="insertUser" parameterType="User">
insert into users (id, username, password)
values (#{id}, #{username}, #{password})
</insert>
x
<insert id="insertUser" parameterType="User">
insert into users (id, username, password)
values (#{id}, #{username}, #{password})
</insert>
<mapper namespace="dulk.learn.mybatis.dao.GirlDao">
<!--定义resultMap-->
<resultMap id="girlResultMap" type="dulk.learn.mybatis.pojo.Girl">
<!--id表查询结果中的唯一标识-->
<id property="id" column="id" />
<!--result表示对普通列名的映射,propertyi表对象属性名,column表查询出的列名-->
<result property="age" column="age" />
</resultMap>
<!--引用外部resultMap,即girlResultMap-->
<select id="findById" parameterType="long" resultMap="girlResultMap">
SELECT * FROM girl WHERE id = #{id}
</select>
</mapper>
x
<mapper namespace="dulk.learn.mybatis.dao.GirlDao">
<!--定义resultMap-->
<resultMap id="girlResultMap" type="dulk.learn.mybatis.pojo.Girl">
<!--id表查询结果中的唯一标识-->
<id property="id" column="id" />
<!--result表示对普通列名的映射,propertyi表对象属性名,column表查询出的列名-->
<result property="age" column="age" />
</resultMap>
<!--引用外部resultMap,即girlResultMap-->
<select id="findById" parameterType="long" resultMap="girlResultMap">
SELECT * FROM girl WHERE id = #{id}
</select>
</mapper>
<!--使用了useGeneratedKeys和keyProperty来将生成的主键设置到对象属性中-->
<insert id="insertGirl" parameterType="dulk.learn.mybatis.pojo.Girl" useGeneratedKeys="true" keyProperty="id">
INSERT INTO girl (age)
VALUES (#{age})
</insert>
<update id="updateGirl" parameterType="dulk.learn.mybatis.pojo.Girl">
UPDATE girl
SET age = #{age}
WHERE id = #{id}
</update>
<delete id="deleteGirl" parameterType="long">
DELETE FROM girl
WHERE id = #{id}
</delete>
x
<!--使用了useGeneratedKeys和keyProperty来将生成的主键设置到对象属性中-->
<insert id="insertGirl" parameterType="dulk.learn.mybatis.pojo.Girl" useGeneratedKeys="true" keyProperty="id">
INSERT INTO girl (age)
VALUES (#{age})
</insert>
<update id="updateGirl" parameterType="dulk.learn.mybatis.pojo.Girl">
UPDATE girl
SET age = #{age}
WHERE id = #{id}
</update>
<delete id="deleteGirl" parameterType="long">
DELETE FROM girl
WHERE id = #{id}
</delete>
public class Test {
@org.junit.Test
public void testMyBatis() throws IOException {
//读取配置文件
InputStream is = Resources.getResourceAsStream("mybatis-config.xml");
//获取工厂类
SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(is);
//获取SqlSession数据库会话对象
SqlSession sqlSession = factory.openSession();
//获取Dao
GirlDao girlDao = sqlSession.getMapper(GirlDao.class);
Girl girl = new Girl();
girl.setAge(18);
//insert a girl with age 18
int resultInsert = girlDao.insertGirl(girl);
Assert.assertEquals(1, resultInsert);
Assert.assertEquals(18, girlDao.findById(girl.getId()).getAge());
//update girl‘s age from 18 to 20
girl.setAge(20);
int resultUpdate = girlDao.updateGirl(girl);
Assert.assertEquals(1, resultUpdate);
Assert.assertEquals(20, girlDao.findById(girl.getId()).getAge());
//delete girl
int resultDelete = girlDao.deleteGirl(girl.getId());
Assert.assertEquals(1, resultDelete);
Assert.assertNull(girlDao.findById(girl.getId()));
}
}
public class Test {
junit.Test .
public void testMyBatis() throws IOException {
//读取配置文件
InputStream is = Resources.getResourceAsStream("mybatis-config.xml");
//获取工厂类
SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(is);
//获取SqlSession数据库会话对象
SqlSession sqlSession = factory.openSession();
//获取Dao
GirlDao girlDao = sqlSession.getMapper(GirlDao.class);
Girl girl = new Girl();
girl.setAge(18);
//insert a girl with age 18
int resultInsert = girlDao.insertGirl(girl);
Assert.assertEquals(1, resultInsert);
Assert.assertEquals(18, girlDao.findById(girl.getId()).getAge());
//update girl‘s age from 18 to 20
girl.setAge(20);
int resultUpdate = girlDao.updateGirl(girl);
Assert.assertEquals(1, resultUpdate);
Assert.assertEquals(20, girlDao.findById(girl.getId()).getAge());
//delete girl
int resultDelete = girlDao.deleteGirl(girl.getId());
Assert.assertEquals(1, resultDelete);
Assert.assertNull(girlDao.findById(girl.getId()));
}
}
<mapper namespace="dulk.learn.mybatis.dao.GirlDao">
<resultMap id="girlResultMap" type="dulk.learn.mybatis.pojo.Girl">
<id property="id" column="id" />
<result property="age" column="age" />
<result property="cupSize" column="cup_size" />
</resultMap>
<!--定义可重用的sql片段-->
<sql id="girlColumn">
id, age
</sql>
<select id="findById" parameterType="long" resultMap="girlResultMap">
<!--通过include引用外部sql片段-->
SELECT <include refid="girlColumn" />
FROM girl WHERE id = #{id}
</select>
</mapper>
x
<mapper namespace="dulk.learn.mybatis.dao.GirlDao">
<resultMap id="girlResultMap" type="dulk.learn.mybatis.pojo.Girl">
<id property="id" column="id" />
<result property="age" column="age" />
<result property="cupSize" column="cup_size" />
</resultMap>
<!--定义可重用的sql片段-->
<sql id="girlColumn">
id, age
</sql>
<select id="findById" parameterType="long" resultMap="girlResultMap">
<!--通过include引用外部sql片段-->
SELECT <include refid="girlColumn" />
FROM girl WHERE id = #{id}
</select>
</mapper>
标签:oca unset racket correct efi 测试 告诉 出现 rom
原文地址:https://www.cnblogs.com/deng-cc/p/9334836.html