MyBatis框架中常用配置
1.Mybatis配置文件mybatis-config.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> <!--驼峰命名转换:goods_id 转换为 goodsId--> <settings> <setting name="mapUnderscoreToCamelCase" value="true"/> </settings> <environments default="dev"> <!--设置默认指向的数据库--> <environment id="dev"> <!--配置环境,不同的环境不同的id名字--> <!--采用JDBC方式对数据库事务进行commit/rollback--> <transactionManager type="JDBC"></transactionManager> <!--采用连接池方式管理数据库连接--> <dataSource type="POOLED"> <property name="driver" value="com.mysql.jdbc.Driver"/> <property name="url" value="jdbc:mysql://localhost:3306/babytun?useUnicode=true& character_set_server =utf8mb4 &serverTimezone=Asia/Shanghai&"/> <property name="username" value="root"/> <property name="password" value="bngd0123.kl"/> </dataSource> </environment> <environment id="prd"> <!--prd通常指代生产,线上环境--> <!--采用JDBC方式对数据库事务进行commit/rollback--> <transactionManager type="JDBC"></transactionManager> <!--采用连接池方式管理数据库连接--> <dataSource type="POOLED"> <property name="driver" value="com.mysql.jdbc.Driver"/> <property name="url" value="jdbc:mysql://192.168.1.155:3306/babytun?useUnicode=true&characterEncoding=UTF-8mb4 &serverTimezone=Asia/Shanghai&"/> <property name="username" value="root"/> <property name="password" value="bngd0123.kl"/> </dataSource> </environment> </environments> <mappers> <mapper resource="mappers/goods.xml"/> </mappers> </configuration>
1.2 MyBatis使用C3P0连接池+PageHelper插件
标签差异: <jdbcUrl> <driverClass> <user>
<?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> <settings> <!-- goods_id ==> goodsId 驼峰命名转换 --> <setting name="mapUnderscoreToCamelCase" value="true"/> </settings> <!--启用Pagehelper分页插件--> <plugins> <plugin interceptor="com.github.pagehelper.PageInterceptor"> <!--设置数据库类型--> <property name="helperDialect" value="mysql"/> <!--分页合理化--> <property name="reasonable" value="true"/> </plugin> </plugins> <!--设置默认指向的数据库--> <environments default="dev"> <!--配置环境,不同的环境不同的id名字--> <environment id="dev"> <!-- 采用JDBC方式对数据库事务进行commit/rollback --> <transactionManager type="JDBC"></transactionManager> <!--采用连接池方式管理数据库连接--> <!--<dataSource type="POOLED">--> <dataSource type="com.imooc.mybatis.datasource.C3P0DataSourceFactory"> <property name="driverClass" value="com.mysql.jdbc.Driver"/> <property name="jdbcUrl" value="jdbc:mysql://localhost:3306/babytun?useUnicode=true&characterEncoding=UTF-8"/> <property name="user" value="root"/> <property name="password" value="root"/> <property name="initialPoolSize" value="5"/> <property name="maxPoolSize" value="20"/> <property name="minPoolSize" value="5"/> <!--...--> </dataSource> </environment> <environment id="prd"> <!-- 采用JDBC方式对数据库事务进行commit/rollback --> <transactionManager type="JDBC"></transactionManager> <!--采用连接池方式管理数据库连接--> <dataSource type="POOLED"> <property name="driver" value="com.mysql.jdbc.Driver"/> <property name="url" value="jdbc:mysql://192.168.1.155:3306/babytun?useUnicode=true&characterEncoding=UTF-8"/> <property name="username" value="root"/> <property name="password" value="root"/> </dataSource> </environment> </environments> <mappers> <mapper resource="mappers/goods.xml"/> <mapper resource="mappers/goods_detail.xml"/> </mappers> </configuration>
2. MyBatis中Mapper 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="goods"> <select id="selectAll" resultType="com.midea.mybatis.com.midea.mybatis.entity.Goods"> select * from t_goods order by goods_id desc limit 10 </select> </mapper>
3.MyBatis中logback.xml文件
<?xml version="1.0" encoding="UTF-8"?> <configuration> <appender name="console" class="ch.qos.logback.core.ConsoleAppender"> <encoder> <pattern>[%thread] %d{HH:mm:ss.SSS} %-5level %logger{36} - %msg%n</pattern> </encoder> </appender> <!-- 日志输出级别(优先级高到低): error: 错误 - 系统的故障日志 warn: 警告 - 存在风险或使用不当的日志 info: 一般性消息 debug: 程序内部用于调试信息 trace: 程序运行的跟踪信息 --> <root level="debug"> <appender-ref ref="console"/> </root> </configuration>
4.MyBatis注解开发
一:sqlSession.getMapper()方法传入【接口类】为参数时,自动生成对应的实现
实现过程依托于XML与接口对应而书写的配置
二:XML书写配置应满足的条件:
- namespace与该接口的包名.接口名一致
- id与接口的方法名一致
- parameterType与接口方法的参数类型一致
- resultType与接口方法的返回值类型一致
三:MyBatis通过sqlSession.getMapper(接口名.class)自动生成实现类从而简化DAO的书写