Mybatis使用Mapper代理模式开发
使用动态代理, 动态生成了Dao的实现类, Mybatis推荐Dao接口取名以Mapper,
UserDao --> UserMapper
要求:
1) Sql映射文件namespace写法必须与对应的Mapper接口的包名.接口名
com.zl.dao.UserMapper namespace=” com.zl.dao.UserMapper”
2) Sql映射文件中statement的id 与对应方法名一致,id写的方法名
3) Statement的parameterType的数据类型与对应方法的参数类型一致
创建资源文件夹管理文件:
src就是资源文件夹,创建资源文件夹会拷贝到bin目录下
mybatis02.mapper包下的:UserMapper.java
public interface UserMapper { // 查询所有用户 List<User> findAll() throws Exception; }
config资源文件夹下的:UserMapper.xml配置文件
<mapper namespace="com.mybatis.mapper.UserMapper"> <!-- 查询所有用户 --> <select id="findAll" resultType="com.mybatis.entity.User"> select * from t_user </select> </mapper>
测试代码:
public static void main(String[] args) throws Exception { SqlSession sqlSession = MybatisUtil.openSession(); UserMapper userMapper = sqlSession.getMapper(UserMapper.class); List<User> list = userMapper.findAll(); for (User user : list) { System.out.println(user); } }
相关推荐
dongxurr 2020-08-08
Dullonjiang 2020-07-30
liqiancao 2020-07-26
helloxusir 2020-07-08
mituan 2020-06-14
xiuyangsong 2020-11-16
Nishinoshou 2020-11-09
jimgreatly 2020-09-01
dongxurr 2020-08-18
Dullonjiang 2020-08-15
Dullonjiang 2020-08-11
Dullonjiang 2020-08-09
yunzhonmghe 2020-08-07
jimgreatly 2020-08-03
jimgreatly 2020-07-27
xiuyangsong 2020-07-26
dongxurr 2020-07-26
mcvsyy 2020-07-26