mybatis
CURD
1.namespace
namespace中的包名要和Dao/mapper接口中的报名一致!
2.select
选择,查询语句
- id:对应的namespace中的方法名
- resultType:Sql语句执行的返回值!
- parameterType:参数类型
3.Insert
<insert id="addUser" parameterType="com.zhang.pojo.User" >
insert into jwxt.user(id, name, pwd)values(#{id},#{name},#{pwd});
</insert>
4.update
update jwxt.user set name=#{name},pwd=#{pwd} where id=#{id};
5.Delete
<delete id="deleteUser" parameterType="map">
delete from jwxt.user where id=#{userid}
</delete>
Map
int updateUser2(Map<String,Object> map);
<update id="updateUser2" parameterType="map">
update jwxt.user set pwd=#{pwd} where id=#{userid};
</update>
public void updateUser2() {
SqlSession sqlSession = MybatisUtils.getsqlsession();
UserMapper mapper = sqlSession.getMapper(UserMapper.class);
HashMap<String,Object>map=new HashMap<String, Object>();
map.put("userid",4);
map.put("pwd",98778963);
mapper.updateUser2(map);
sqlSession.commit();
sqlSession.close();
}
生命周期
ResultMap
<resultMap id="blogResult" type="Blog">
<id property="id" column="blog_id" />
<result property="title" column="blog_title"/>
<collection property="posts" ofType="Post" resultMap="blogPostResult" columnPrefix="post_"/>
</resultMap>
<resultMap id="blogPostResult" type="Post">
<id property="id" column="id"/>
<result property="subject" column="subject"/>
<result property="body" column="body"/>
</resultMap>
日志
Log4j
1.导入包import org.apache.log4j.Logger;
- logger.info
- logger.debug
- logger.error
2.日志对象
logger.getlogger(UserDaoTest.class)
常用配置
#将等级为DEBUG的日志信息输出到console和file这两个目的地,console和file的定义在下面的代码 log4j.rootLogger=DEBUG,console,file
#控制台输出的相关设置 log4j.appender.console = org.apache.log4j.ConsoleAppender log4j.appender.console.Target = System.out log4j.appender.console.Threshold=DEBUG log4j.appender.console.layout = org.apache.log4j.PatternLayout log4j.appender.console.layout.ConversionPattern=[%c]-%m%n
#文件输出的相关设置 log4j.appender.file = org.apache.log4j.RollingFileAppender log4j.appender.file.File=./log/kuang.log log4j.appender.file.MaxFileSize=10mb log4j.appender.file.Threshold=DEBUG log4j.appender.file.layout=org.apache.log4j.PatternLayout log4j.appender.file.layout.ConversionPattern=[%p][%d{yy-MM-dd}][%c]%m%n
#日志输出级别 log4j.logger.org.mybatis=DEBUG log4j.logger.java.sql=DEBUG log4j.logger.java.sql.Statement=DEBUG log4j.logger.java.sql.ResultSet=DEBUG log4j.logger.java.sql.PreparedStatement=DEBUG