Mybatis之动态代理配置
一:Mybatis配置步骤(动态代理实现):
1)引入maven坐标,及slf4j坐标
<!--mybatis-->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.2.8</version>
</dependency>
<!--日志-->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.6.4</version>
</dependency>
2)引入jdbc.properties及log4j.properties文件
log4j.properties:
log4j.rootLogger=DEBUG,A1
log4j.logger.com.taotao = DEBUG
log4j.logger.org.mybatis = DEBUG
log4j.appender.A1=org.apache.log4j.ConsoleAppender
log4j.appender.A1.layout=org.apache.log4j.PatternLayout
log4j.appender.A1.layout.ConversionPattern=%-d{yyyy-MM-dd HH:mm:ss,SSS} [%t] [%c]-[%p] %m%n
jdbc.properties:
jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://127.0.0.1:3306/mybatis_db?useUnicode=true&characterEncoding=utf8&autoReconnect=true&allowMultiQueries=true
jdbc.username=root
jdbc.password=123
3)引入mybatis的全局配置文件(注意:properties/settings/... 标签出现的顺序不能错)
<?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>
<properties resource="jdbc.properties" />
<settings>
<!--设置开启驼峰标识: 命名规则 碰到 user_name 解析成 userName-->
<setting name="mapUnderscoreToCamelCase" value="true"/>
</settings>
<typeAliases>
<!--方式一: 给指定类起别名, 适用场景:给比较少的类起别名-->
<!--<typeAlias type="cn.itcast.pojo.User" alias="User"/>-->
<!-- 设置别名:为了让sql的xml中的resultType可以直接写下包下面的存在的Class名称-->
<package name="com.xucj.pojo" />
</typeAliases>
<!-- 配置数据库连接的环境 -->
<environments default="development">
<environment id="development">
<transactionManager type="JDBC" />
<dataSource type="POOLED">
<property name="driver" value="${jdbc.driverClassName}" />
<property name="url" value="${jdbc.url}" />
<property name="username" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
</dataSource>
</environment>
<!--测试环境
<environment id="test">
<transactionManager type="JDBC"/>
<dataSource type="POOLED">
<property name="driver" value="${jdbc.driverClassName}"/>
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
</dataSource>
</environment>-->
</environments>
<mappers>
<mapper resource="mybatis/mapper/UserMapper.xml" />
<!--方式二: mapper文件的目录 必须和 mapper接口的package保持一致-->
<!--<package name="cn.itcast.mapper"/>-->
</mappers>
</configuration>
4)创建mapper接口userMapper
public interface UserMapper {
public User findUserById(Long id);
public List<User> findUserAll();
public int deleteUserById(String id);
public int updateUser(User user);
public int addUser(User user);
}
5)创建userMapper.xml文件
<?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="com.xucj.mapper.user.UserMapper">
<select id="findUserById" parameterType="long" resultType="User">
select * from tb_user
where 1=1 and id = #{id}
</select>
</mapper>
6)userMapper.xml和userMapper接口的目录结合mybatis全局配置文件截图说明:
7)测试
public class UserTest {
private UserMapper userMapper;
@Before
public void setUp() throws Exception {
String resource = "mybatis-conf.xml";
InputStream in = Resources.getResourceAsStream(resource);
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(in);
SqlSession sqlSession = sqlSessionFactory.openSession(true);
userMapper = sqlSession.getMapper(UserMapper.class);
}
@Test
public void test1(){
System.out.println(userMapper.findUserById(1L).toString());
}
}
结果:
推荐阅读
-
代理模式之Java动态代理实现方法
-
MyBatis启动之XMLConfigBuilder解析配置文件(二)
-
Spring学习之动态代理(JDK动态代理和CGLIB动态代理)
-
MyBatis从入门到精通(八):MyBatis动态Sql之foreach标签的用法
-
代理模式之Java动态代理实现方法
-
Nginx实战之反向代理WebSocket的配置实例
-
荐 [设计模式] 代理模式之 静态代理与动态代理 & Mybatis实例解析
-
Thinkphp 框架配置操作之动态配置、扩展配置及批量配置实例分析
-
spring boot 之 Mybatis 配置
-
DB数据源之SpringBoot+MyBatis踏坑过程(二)手工配置数据源与加载Mapper.xml扫描