MyBatis学习4之CRUD基于注解
程序员文章站
2022-04-01 20:33:28
...
一、使用MyBatis对t_group表执行CRUD操作——基于注解的实现
1、定义sql映射接口
public interface GroupMapper { @Insert("insert into t_group(group_name, description) values(#{group_name}, #{description})") public int add(Group group); //使用@Delete注解指明deleteById方法要执行的SQL @Delete("delete from t_group where id=#{id}") public int deleteById(int id); //使用@Update注解指明update方法要执行的SQL @Update("update t_group set group_name=#{groupName},description=#{description} where id=#{id}") public int update(Group group); //使用@Select注解指明getById方法要执行的SQL @Select("select * from t_group where id=#{id}") public Group getById(int id); //使用@Select注解指明getAll方法要执行的SQL @Select("select * from t_group") public List<Group> getAll(); }
2、在mybatis.xml文件注册接口
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd"> <configuration> <typeAliases> <typeAlias alias="Group" type="com.zlt.mybatis.model.Group" /> </typeAliases> <environments default="development"> <environment id="development"> <transactionManager type="JDBC" /> <dataSource type="POOLED"> <property name="driver" value="org.sqlite.JDBC" /> <property name="url" value="jdbc:sqlite:cfg/ht.db" /> <property name="username" value="" /> <property name="password" value="" /> </dataSource> </environment> </environments> <mappers> <!-- <mapper resource="com/zlt/mybatis/mapping/GroupMapper.xml" /> --> <mapper class="com.zlt.mybatis.impl.GroupMapped"/> </mappers> </configuration>
3、单元测试
3.1添加用户组信息
public void testAddGroup(){ SqlSession session = null; try{ session = sqlSessionFactory.openSession(); String statement = "com.zlt.mybatis.model.Group.addGroup"; GroupMapper mapper = session.getMapper(GroupMapper.class); Group data = new Group(); data.setGroupName("三国群英"); data.setDescription("三国文化"); mapper.add(data); session.commit(); }finally{ if(null != session) session.close(); } }
运行结果:
3.2更新用户组信息
public void testUpdateGroup(){ SqlSession session = null; try{ session = sqlSessionFactory.openSession(); GroupMapper mapper = session.getMapper(GroupMapper.class); Group data = new Group(); data.setId(6); data.setGroupName("大话三国组"); data.setDescription("三国无厘头"); mapper.update(data); session.commit(); }finally{ if(null != session) session.close(); } }
运行结果:
3.3删除相应组信息
public void testDeleteGroup(){ SqlSession session = null; try{ session = sqlSessionFactory.openSession(); GroupMapper mapper = session.getMapper(GroupMapper.class); mapper.deleteById(2); session.commit(); }finally{ if(null != session) session.close(); } }
运行结果:删除
3.4、查询所有组信息
public void testQueryAllGroup(){ SqlSession session = null; try{ session = sqlSessionFactory.openSession(); GroupMapper mapper = session.getMapper(GroupMapper.class); List<Group> groups = mapper.getAll(); for (Group g : groups) { System.out.println("ID:" + g.getId() + "name:" + g.getGroupName() + "description:" + g.getDescription()); } }finally{ if(null != session) session.close(); } }
运行结果:
ID:1name:nulldescription:Java项目组开发
ID:6name:nulldescription:三国无厘头
下一篇: 创新的Eclipse插件,用于代码探测