目录 1、mybatis环境搭建 2、mybatis dao接口实现 3、动态代理方式,只实现Mapper接口
mybatis dao层实现1. 实现dao层接口
2. 接口代理方式实现dao代理开发要求: Mapper接口开发方法只需要编写Mapper接口(相当于dao接口),由mybatis框架根据接口定义创建接口的动态代理对象,代理对象的方法体和dao方式实现的接口一样。1. mapper.xml 中namespace与mapper接口中全限定名相同
2. mapper接口方法名和mapper.xml中定义的每个statement的id相同
3. mapper接口方法入参类型,和 statement的paramType相同
4. mapper接口返回值类型,和statement的resultType相同
1、mybatis环境搭建
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapperPUBLIC "-//mybatis.org//DTD Mapper 3.0//EN""http://mybatis.org/dtd/mybatis-3-mapper.dtd"> < mapper namespace = " cn.bitqian.dao.UserMapper" > < select id = " queryAllUser" resultType = " user" > select * from users1</ select> < select id = " queryUserByUserId" parameterType = " int" resultType = " user" > select * from users1 where userid = #{id}</ select> </ mapper>
mybatis-config.xml 核心配置文件
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configurationPUBLIC "-//mybatis.org//DTD Config 3.0//EN""http://mybatis.org/dtd/mybatis-3-config.dtd"> < configuration> < properties resource = " jdbc.properties" > </ properties> < typeAliases> < typeAlias type = " cn.bitqian.entity.User" alias = " user" > </ typeAlias> </ typeAliases> < environments default = " development" > < environment id = " development" > < transactionManager type = " JDBC" > </ transactionManager> < 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> < mappers> < mapper resource = " cn\bitqian\mapper\user-mapper.xml" > </ mapper> </ mappers> </ configuration>
2、mybatis dao接口实现
package cn. bitqian. dao; import cn. bitqian. entity. User; import java. io. IOException;
import java. util. List;
public interface UserMapper { public List< User> queryAllUser ( ) throws IOException; public User queryUserByUserId ( int userId) ; }
package cn. bitqian. dao. impl; import cn. bitqian. dao. UserMapper;
import cn. bitqian. entity. User;
import org. apache. ibatis. io. Resources;
import org. apache. ibatis. session. SqlSession;
import org. apache. ibatis. session. SqlSessionFactory;
import org. apache. ibatis. session. SqlSessionFactoryBuilder; import java. io. IOException;
import java. io. InputStream;
import java. util. List;
public class UserMapperImpl implements UserMapper { @Override public List< User> queryAllUser ( ) throws IOException { InputStream resourceAsStream = Resources. getResourceAsStream ( "mybatis-config.xml" ) ; SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder ( ) . build ( resourceAsStream) ; SqlSession sqlSession = sqlSessionFactory. openSession ( ) ; List< User> userList = sqlSession. selectList ( "cn.bitqian.dao.UserMapper.queryAllUser" ) ; sqlSession. close ( ) ; return userList; } @Override public User queryUserByUserId ( int userId) { return null; }
}
3、动态代理方式,只实现Mapper接口
优点分析:1. 只需实现mapper接口,定义对应的业务方法,注意接口名,
方法名,入参,返回的参数和mappers的配置一致2. 无需实现mapper接口,mybatis通过动态代理的方式进行实现了
只需用UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
getMapper api 来创建对象,进行数据库操作
package cn. bitqian. service; import cn. bitqian. dao. UserMapper;
import cn. bitqian. dao. impl. UserMapperImpl;
import cn. bitqian. entity. User;
import org. apache. ibatis. io. Resources;
import org. apache. ibatis. session. SqlSession;
import org. apache. ibatis. session. SqlSessionFactory;
import org. apache. ibatis. session. SqlSessionFactoryBuilder; import java. io. IOException;
import java. io. InputStream;
import java. util. List;
public class DemoTest { public static void main ( String[ ] args) { UserMapper userMapper = new UserMapperImpl ( ) ; try { System. out. println ( userMapper. queryAllUser ( ) ) ; } catch ( IOException e) { e. printStackTrace ( ) ; } test1 ( ) ; test2 ( ) ; } private static UserMapper userMapper; static { try { InputStream resourceAsStream = Resources. getResourceAsStream ( "mybatis-config.xml" ) ; SqlSessionFactory build = new SqlSessionFactoryBuilder ( ) . build ( resourceAsStream) ; SqlSession sqlSession = build. openSession ( ) ; userMapper = sqlSession. getMapper ( UserMapper. class ) ; } catch ( IOException e) { e. printStackTrace ( ) ; } } static void test1 ( ) { try { List< User> userList = userMapper. queryAllUser ( ) ; System. out. println ( userList) ; } catch ( IOException e) { e. printStackTrace ( ) ; } } static void test2 ( ) { User user = userMapper. queryUserByUserId ( 1 ) ; System. out. println ( user) ; } }