框架学习只需要知道怎么做就好。
首先看下今天学习mybatis的大致架构
先两个xml配置文件
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>
<setting name="logImpl" value="LOG4J"/>
</settings>
<environments default="development">
<environment id="development">
<transactionManager type="JDBC"/>
<!-- POOLED是使用数据连接池,前面加个UN就是不使用-->
<dataSource type="POOLED">
<property name="driver" value="com.mysql.cj.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/mybatis?serverTimezone=UTC"/>
<property name="username" value="root"/>
<property name="password" value="123456"/>
</dataSource>
</environment>
</environments>
<mappers>
<!-- 这里的写的是mapper映射文件,必须要写-->
<mapper resource="mybatis/mappers/test/moneyMapper.xml"/>
<mapper resource="mybatis/mappers/stuinfo/studentMapper.xml"/>
</mappers>
</configuration>
package Day8.mybatis;
public class money {
private Integer id;
private String typein;
private Double amount;
private String timein;
private Double balance;
//这应该是个标准的javabean类,ptg插件生成的set,get,构造器省略不写了
}
Mappers.xml用于存放执行的sql语句实现解耦
<?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="moneyMapper">
<!-- 查询功能的标签必须设置resultType或resultMap-->
<!-- resultType:设置默认的映射关系-->
<!-- resultMap:设置自定义的映射关系-->
<update id="insert">
insert into money values(null,#{typein},#{amount},#{timein},#{balance})
</update>
<update id="delete">
delete from money where id=#{id}
</update>
<update id="update">
update money set typein=#{typein},amount=#{amount} where id=14
</update>
<select id="selectById" resultType="Day8.mybatis.money">
select *
from `money`
where `id` = ${id}
</select>
<select id="selectAll" resultType="Day8.mybatis.money">
select *
from `money`
</select>
</mapper>
下面的就是测试代码了
package Day8.mybatis;
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 org.apache.log4j.PropertyConfigurator;
import org.junit.Test;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Properties;
public class testMybatis {
public static SqlSession session;
static {
try {
session = getSession();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
//获取连接会话对象
public static SqlSession getSession() throws IOException {
InputStream in = Resources.getResourceAsStream("mybatis/config/mybatis_config.xml");
SqlSessionFactory fac = new SqlSessionFactoryBuilder().build(in);
SqlSession sqlSession = fac.openSession();
return sqlSession;
}
//输出日志封装方法log4j
public static void logOut() throws IOException {
Properties properties = new Properties();
FileInputStream in = new FileInputStream("src/resources/log4j.properties");
properties.load(in);
PropertyConfigurator.configure(properties);
}
@Test
public void insert() throws IOException {
testMybatis.logOut();
HashMap<String, Object> maps = new HashMap<>();
maps.put("typein","qqq");
maps.put("amount",11111.0);
maps.put("timein","2024-8-16 12:00");
maps.put("balance",100.0);
//这里需要注意的是,增加,删除,修改都调用的都是--->>>>会话对象.update()方法
int update = session.update("moneyMapper.insert",maps);
session.commit();
System.out.println("update = " + update);
}
//动态传值
@Test
public void selectById() throws IOException {
testMybatis.logOut();
HashMap hashMap = new HashMap();
hashMap.put("id",12);
List<money> obj = getSession().selectList("moneyMapper.selectById",hashMap);
for (money o : obj) {
System.out.println(o);
}
}
}
今日问题1:
我的环境是jdk1.8+maven3.9.3+mybatis3.55+log4j1.2.12
Mybatis框架下Log4j配置文件写好,加载语句也完成,控制台不输出错误,也不输出日志解决方案:
在mybatis_config.xml的configuration标签中加一句,就可完美解决
<settings>
<setting name="logImpl" value="LOG4J"/>
</settings>