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>
<settings>
<!-- goods_id ==> goodsId 驼峰命名转换 -->
<setting name="mapUnderscoreToCamelCase" value="true"/>
</settings>
<!--设置默认指向的数据库-->
<environments default="dev">
<!--配置环境,不同的环境不同的id名字-->
<environment id="dev">
<!-- 采用JDBC方式对数据库事务进行commit/rollback -->
<transactionManager type="JDBC"></transactionManager>
<!--采用连接池方式管理数据库连接-->
<dataSource type="POOLED">
<property name="driver" value="com.mysql.cj.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3307/imooc_oa?useSSL=false&useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai&allowPublicKeyRetrieval=true"/>
<property name="username" value="root"/>
<property name="password" value="root"/>
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource="mappers/test.xml"/>
</mappers>
</configuration>
test.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="test">
<select id="sample" resultType="String">
select 'success'
</select>
</mapper>
MybatisUtils
com.imooc.oa.utilsorg.apache.ibatis.io.Resourcesorg.apache.ibatis.session.SqlSessionorg.apache.ibatis.session.SqlSessionFactoryorg.apache.ibatis.session.SqlSessionFactoryBuilderjava.io.IOExceptionjava.io.Readerjava.util.function.FunctionMybatisUtils {
SqlSessionFactory = {
Reader reader = {
reader = Resources.()= SqlSessionFactoryBuilder().build(reader)} (IOException e) {
e.printStackTrace()ExceptionInInitializerError(e)}
}
Object (Function<SqlSessionObject> func){
SqlSession sqlSession = .openSession(){
Object obj = func.apply(sqlSession)obj}{
sqlSession.close()}
}
}
Testor
com.imooc.oa.utils.MybatisUtilsorg.junit.MybatisUtilsTestor {
(){
MybatisUtils.(sqlSession -> {
String out = (String) sqlSession.selectOne()out})}
}
我的3306被占用了,所以用的3307
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星