目录
log4j 日志显示 sql语句 作用是查看 debug sql 语句
给类起别名
为了在 StudentMapper.xml 中 resultType="com.yuyu.bean.Student" 太多的时候方便管理
<select id="findById" parameterType="int" resultType="com.yuyu.bean.Student">
select * from student where studentid=#{id}
</select>
在 mybatis.xml 中给类起别名
<typeAliases>
<!—给实体类起别名 -->
<typeAlias type="com.yuyu.bean.Student" alias="stu"></typeAlias> <!-- 这样在 StudentMapper.xml 中使用时可以只写别名 stu -->
<!--指定哪些包的类可以使⽤别名,默认别名:类名⾸字⺟⼩写(实际使⽤的时候,全部⼩写也可以做结果映射) -->
<package name="com.yuyu.bean"></package> <!-- 这样在 StudentMapper.xml 中使用时可以只写 bean 包下类的名称(类名⾸字⺟⼩写) -->
</typeAliases>
获得新增数据的id
适⽤于可以⾃增的主键列上
<insert useGeneratedKeys="true" keyProperty="userid">
通过自增的方式获取 获得的值设置映射到程序上的名称
<insert id="insertStudent" parameterType="com.yuyu.bean.Student" useGeneratedKeys="true" keyProperty="studentId">
insert into student(studentno,stuname) values(#{studentNo},#{stuName})
</insert>
Student student = new Student();
student.setStudentNo("s00114");
student.setStuName("谢大脚4");
int insert = session.insert("com.yuyu.dao.StudentDao.insertStudent", student);
session.commit();
System.out.println("insert="+insert);
//通过 mapper.xml 中的 keyProperty 得到新增数据 id
System.out.println("studentid="+student.getStudentId());
log4j 日志显示 sql语句 作用是查看 debug sql 语句
1.添加 jar 包
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.5</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.7.12</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
2.配置 log4j.properties 文件
log4j.rootLogger=DEBUG, Console
log4j.appender.Console=org.apache.log4j.ConsoleAppender
log4j.appender.Console.layout=org.apache.log4j.PatternLayout
log4j.appender.Console.layout.ConversionPattern=%d [%t] %-5p [%c] - %m%n
log4j.logger.java.sql.ResultSet=INFO
log4j.logger.org.apache=INFO
log4j.logger.java.sql.Connection=DEBUG
log4j.logger.java.sql.Statement=DEBUG
log4j.logger.java.sql.PreparedStatement=DEBUG