多应用+插件架构,代码干净,二开方便,首家独创一键云编译技术,文档视频完善,免费商用码云13.8K 广告
## 配置 ~~~ <!--配置别名--> <typeAliases> <package name="com.like.domain"/> </typeAliases> <mappers> <package name="com.like.dao"/> </mappers> ~~~ ## 查询所有 ~~~ @Select("select * from user") List<User> findAll(); ~~~ ~~~ IUserDao mapper = session.getMapper(IUserDao.class); List<User> users = mapper.findAll(); System.out.println(users); ~~~ ## 查询一个 ~~~ @Select("select * from user where id = #{id}") User findById(Integer id); ~~~ ~~~ IUserDao mapper = session.getMapper(IUserDao.class); User user = mapper.findById(48); System.out.println(user); ~~~ ## 模糊查 ~~~ @Select("select * from user where username like #{username}") List<User> findByUsername(String username); ~~~ ~~~ IUserDao mapper = session.getMapper(IUserDao.class); List<User> users = mapper.findByUsername("%王%"); System.out.println(users); ~~~ ## 聚合查 ~~~ @Select("select count(*) from user") int findTotal(); ~~~ ~~~ IUserDao mapper = session.getMapper(IUserDao.class); int total = mapper.findTotal(); System.out.println(total); ~~~ ## 更新 ~~~ @Update("update user set username = #{username} , sex = #{sex}, address = #{address} where id = #{id}") void updateUser(User user); ~~~ ~~~ IUserDao mapper = session.getMapper(IUserDao.class); User user = new User(); user.setId(59); user.setUsername("milan"); user.setAddress("江苏"); user.setBirthday(new Date()); user.setSex("女"); mapper.updateUser(user); session.commit(); session.close(); ~~~ ## 删除 ~~~ @Delete("delete from user where id = #{id}") void deleteUser(User user); ~~~ ~~~ IUserDao mapper = session.getMapper(IUserDao.class); User user = new User(); user.setId(57); mapper.deleteUser(user); session.commit(); session.close(); ~~~ ## 增加 ~~~ @Select("select * from user") List<User> findAll(); ~~~ ~~~ IUserDao mapper = session.getMapper(IUserDao.class); User user = new User(); user.setUsername("jack"); user.setAddress("浙江"); user.setBirthday(new Date()); user.setSex("男"); mapper.saveUser(user); session.commit(); ~~~