mybatis配置相关代码
配置
<?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="mysql.properties"/>
<environments default="development">
<environment id="development">
<transactionManager type="JDBC"/>
<dataSource type="POOLED">
<property name="driver" value="${driver}"/>
<property name="url" value="${url}"/>
<property name="username" value="${user}"/>
<property name="password" value="${password}"/>
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource="dao/UserMapper.xml"/>
</mappers>
</configuration>
工具类
package utilts;
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.springframework.core.io.Resource;
import java.io.IOException;
import java.io.InputStream;
import static org.apache.ibatis.io.Resources.getResourceAsStream;
public class MybatisUtils {
private static SqlSessionFactory sqlSessionFactory;
static {
try {
String resource="mybatis-config.xml";
InputStream in= Resources.getResourceAsStream(resource);
sqlSessionFactory=new SqlSessionFactoryBuilder().build(in);
} catch (IOException e) {
e.printStackTrace();
}
}
public static SqlSession getSqlsession(){
return sqlSessionFactory.openSession(true);
}
}
映射实现
<?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="dao.UserMapper">
<insert id="add" parameterType="pojo.User">
insert into home.user values (#{id},#{name},#{age})
</insert>
<delete id="delete" parameterType="String">
delete from home.user where id=#{id}
</delete>
<update id="update" parameterType="pojo.User">
update user
<set>
<if test="id!=null and id!=''">id=#{id},</if>
<if test="name!=null and name!=''" >name=#{name},</if>
<if test="age!=null and age!=''" >age=#{age}</if>
</set>
where id=#{id}
</update>
<select id="select" resultType="pojo.User">
select * from user
</select>
</mapper>
此次遇到的问题
servlet映射路径未找到报404
这个问题实属无语
问题的关键在于应用上下文,程序执行之后页面实际地址为http://localhost:8081/add,但是因为tomcat里面的上下文为/MavenSon3_war_exploded,所以事实上得跳转到
http://localhost:8081/MavenSon3_war_exploded/add才可以找到
解决方案将应用程序上下文改为空
其次就是tomcat10和tomcat9的问题了,本人装了两个tomcat,端口分别为8080和8081,本次如果是tomcat9则会报一个异常,而tomcat10则不会
网上查找资料说是jar包和tomcat版本不匹配的问题,由于本人最近才开始使用idea,之前用eclipse并未碰到如此问题,发现idea的版本匹配机制挺迷糊的
之前jdk和maven版本也出现了不匹配问题
另外切记程序在运行之前一定要注册mapper
标签:改查,ibatis,增删,io,import,mybatis,org,apache,id From: https://www.cnblogs.com/liyiyang/p/17034276.html