这2种方法的区别:1、添加接口 2、运用接口来实现
其他的都一样
添加接口
//接口的名字和xml的名字一样,这样xml中的namespace就不用改 public interface UserMapper { //接口名的方法名一定要和xml中的id名一样 public int count(); public void add(User user); public void update(User user); public void delete(User user); public List<User> getUserList(); }
添加接口的测试类也很简单(看下注释的差别)
public class UserDaoByMapper { private Logger logger = Logger.getLogger(UserDaoTest.class); //count @Test public void countTest() { SqlSession sqlSession = null; int count = 0; try { sqlSession = MyBatisUtil.createSqlSession(); // count = sqlSession.selectOne("cn.bdqn.dao.UserMapper.count"); //有接口后,用这个 count = sqlSession.getMapper(UserMapper.class).count(); logger.debug("count==="+count); } catch (Exception e) { // TODO: handle exception sqlSession.rollback(); }finally{ MyBatisUtil.closeSqlSession(sqlSession); } } //add @Test public void addTest(){ SqlSession sqlSession = null; try { User user = new User(); user.setUserCode("aaaa"); user.setUserName("bbbbb"); sqlSession = MyBatisUtil.createSqlSession(); // sqlSession.insert("cn.bdqn.dao.UserMapper.add",user); sqlSession.getMapper(UserMapper.class).add(user); sqlSession.commit(); } catch (Exception e) { // TODO: handle exception e.printStackTrace(); sqlSession.rollback(); }finally{ MyBatisUtil.closeSqlSession(sqlSession); } } //修改 @Test public void updateTest(){ SqlSession sqlSession = null; try { User user = new User(); user.setId(12); user.setUserCode("改1111"); user.setUserName("改2222"); sqlSession = MyBatisUtil.createSqlSession(); // sqlSession.update("cn.bdqn.dao.UserMapper.update",user); sqlSession.getMapper(UserMapper.class).update(user); sqlSession.commit(); } catch (Exception e) { // TODO: handle exception e.printStackTrace(); sqlSession.rollback(); }finally{ MyBatisUtil.closeSqlSession(sqlSession); } } //删除 @Test public void deleteTest(){ SqlSession sqlSession = null; try { User user = new User(); user.setId(12); sqlSession = MyBatisUtil.createSqlSession(); // sqlSession.delete("cn.bdqn.dao.UserMapper.delete",user); sqlSession.getMapper(UserMapper.class).delete(user); sqlSession.commit(); } catch (Exception e) { // TODO: handle exception e.printStackTrace(); sqlSession.rollback(); }finally{ MyBatisUtil.closeSqlSession(sqlSession); } } //查询 @Test public void getUserListTest(){ SqlSession sqlSession = null; try { List<User> userList = new ArrayList<User>(); sqlSession = MyBatisUtil.createSqlSession(); // userList = sqlSession.selectList("cn.bdqn.dao.UserMapper.getUserList"); userList = sqlSession.getMapper(UserMapper.class).getUserList(); for(User user:userList){ logger.debug("user的id==="+user.getId()); } } catch (Exception e) { // TODO: handle exception e.printStackTrace(); sqlSession.rollback(); }finally{ MyBatisUtil.closeSqlSession(sqlSession); } } }
<?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+id(方法名)的名字就可以访问 --> <mapper namespace="cn.bdqn.dao.UserMapper"> <!-- id要唯一的,一般是下拉类的方法名 --> <!-- 返回的是什么类型int --> <select id="count" resultType="int"> select count(1) from user </select> <!-- 增加 --> <insert id="add" parameterType="User"> insert into user(userCode,userName,userPassword) values (#{userCode},#{userName},#{userPassword}) </insert> <!-- 修改 --> <update id="update" parameterType="User"> update user set userCode=#{userCode},userName=#{userName}, userPassword=#{userPassword} where id=#{id} </update> <!-- 删除 --> <delete id="delete" parameterType="User"> delete from user where id=#{id} </delete> <!-- 查询 --> <select id="getUserList" resultType="User"> select * from user </select> </mapper>
<?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"> <!-- 通过这个配置文件完成mybatis与数据库的连接 --> <configuration> <!-- 引入 jdbc.properties 文件--> <properties resource="jdbc.properties"/> <!-- alias别名 --> <typeAliases> <!-- <typeAlias type="cn.bdqn.pojo.User" alias="User"/> --> <!-- 用这个比较方便,不用一个一个写。包下的就是他的类名 --> <package name="cn.bdqn.pojo"/> </typeAliases> <environments default="development"> <environment id="development"> <!--配置事务管理,采用JDBC的事务管理 --> <transactionManager type="JDBC"></transactionManager> <!-- POOLED:mybatis自带的数据源,JNDI:基于tomcat的数据源 --> <dataSource type="POOLED"> <property name="driver" value="${driver}"/> <property name="url" value="${url}"/> <property name="username" value="${username}"/> <property name="password" value="${password}"/> </dataSource> </environment> </environments> <!-- 将mapper文件加入到配置文件中 将来mapper文件很多所以是mappers --> <mappers> <mapper resource="cn/bdqn/dao/UserMapper.xml"/> </mappers> </configuration>