回顾-
丰富接口实现更多的操作
public interface IUserDao { public List<User> findALL(); public List<User> findById(int id); public List<User> findByName(String username); public void saveUser(User u); public void updateUser(User u); public void deleteUser(int id); public List<User> findByQueryBean(QueryBean queryBean); }
现在我们一下子添加了对数据的增删改和各种的查询操作
对应的Mapper.xml文件也要增加相应的条目
<mapper namespace="mybatis.CRUD.dao.IUserDao"> <select id="findALL" resultType="mybatis.CRUD.bean.User"> SELECT * FROM user; </select> <select id="findById" resultType="mybatis.CRUD.bean.User" parameterType="int"> SELECT * FROM user WHERE id=#{id} ; </select> <select id="findByName" resultType="mybatis.CRUD.bean.User" parameterType="java.lang.String"> SELECT * FROM USER WHERE username LIKE #{username}; </select> <select id="findByQueryBean" parameterType="mybatis.CRUD.bean.QueryBean" resultType="mybatis.CRUD.bean.User"> sELECT * FROM user WHERE username LIKE #{user.username}; </select> <insert id="saveUser" parameterType="mybatis.CRUD.bean.User"> <selectKey keyProperty="id" keyColumn="id" resultType="int" order="AFTER"> SELECT last_insert_id(); </selectKey> INSERT INTO user(username,birthday,sex,address) VALUES (#{username},#{birthday},#{sex},#{address}); </insert> <update id="updateUser" parameterType="mybatis.CRUD.bean.User"> UPDATE user SET username=#{username},birthday=#{birthday},sex=#{sex},address=#{address} WHERE id=#{id}; </update> <delete id="deleteUser" parameterType="int"> DELETE FROM user WHERE id=#{id}; </delete> </mapper>
另外Mybatis中Sql语句的拼接中有‘#{}’与‘${}’两种形式,其实这两个都是老朋友了
#{}:使用的是PreparedStatement提交sql语句,#{}中的数据就是原来的?对应的位置。
${}:使用的是Statement提交sql语句,${}中的数据会直接拼接进sql语句中。
最后编写测试类测试代码
public class MybatisTest { InputStream in; SqlSession session; IUserDao userDao; @Before public void init()throws Exception{ in= Resources.getResourceAsStream("SqlMapConfig.xml"); SqlSessionFactoryBuilder builder=new SqlSessionFactoryBuilder(); SqlSessionFactory factory=builder.build(in); session=factory.openSession(); userDao=session.getMapper(IUserDao.class); } @After public void destory() throws IOException { session.close(); in.close(); } @Test public void testFindAll() throws Exception { List<User> users=userDao.findALL(); for(User user:users) { System.out.println(user.toString()); } } @Test public void testSaveUser()throws Exception{ User u=new User(); u.setUsername("XX"); u.setSex("男"); u.setAddress("XX"); u.setBirthday(new Date()); System.out.println("保存数据之前的"+u.toString()); userDao.saveUser(u); System.out.println("保存数据之后的"+u.toString()); System.out.println("获得id:"+u.getId()); session.commit(); } @Test public void testUpdateUser()throws Exception{ User u=new User(); u.setId(51); u.setUsername("XXXXX"); u.setSex("男"); u.setAddress("XXXXX"); u.setBirthday(new Date()); userDao.updateUser(u); session.commit(); } @Test public void testDeleteUser()throws Exception{ userDao.deleteUser(48); session.commit(); } @Test public void testFindById() throws Exception { List<User> users=userDao.findById(51); for(User user:users) { System.out.println(user.toString()); } } @Test public void testFindByName() throws Exception { List<User> users=userDao.findByName("YF%"); for(User user:users) { System.out.println(user.toString()); } } @Test public void testFindByQueryBean(){ QueryBean qb=new QueryBean(); User user=new User(); user.setUsername("%X%"); qb.setUser(user); List<User> users=userDao.findByQueryBean(qb); for(User u:users) { System.out.println(u.toString()); } } }
<mapper namespace="mybatis.CRUD.dao.IUserDao">
<select id="findALL" resultType="mybatis.CRUD.bean.User">
SELECT * FROM user;
</select>
<select id="findById" resultType="mybatis.CRUD.bean.User" parameterType="int">
SELECT * FROM user WHERE id=#{id} ;
</select>
<select id="findByName" resultType="mybatis.CRUD.bean.User" parameterType="java.lang.String">
SELECT * FROM USER WHERE username LIKE #{username};
</select>
<select id="findByQueryBean" parameterType="mybatis.CRUD.bean.QueryBean" resultType="mybatis.CRUD.bean.User">
sELECT * FROM user WHERE username LIKE #{user.username};
</select>
<insert id="saveUser" parameterType="mybatis.CRUD.bean.User">
<selectKey keyProperty="id" keyColumn="id" resultType="int" order="AFTER">
SELECT last_insert_id();
</selectKey>
INSERT INTO user(username,birthday,sex,address) VALUES (#{username},#{birthday},#{sex},#{address});
</insert>
<update id="updateUser" parameterType="mybatis.CRUD.bean.User">
UPDATE user SET username=#{username},birthday=#{birthday},sex=#{sex},address=#{address} WHERE id=#{id};
</update>
<delete id="deleteUser" parameterType="int">
DELETE FROM user WHERE id=#{id};
</delete>
</mapper>