第二次课程:
观看视频
链接:https://pan.baidu.com/s/1DQ_FDNSpP8DpbvYrv2x3Ww
提取码:e9c4
视频名称:6—mybatis示例
5 、连接数据库的配置
driver=com.mysql.jdbc.Driver
# url的注意点: 不要用空格;不要出现中文的?;
# 使用unicode编码格式
# 字符集设置utf-8
url=jdbc:mysql://127.0.0.1:3306/smbms?useUnicode=true&characterEncoding=utf-8
user=root
password=root
6、配置log4j的配置文件
log4j.rootLogger=DEBUG,CONSOLE,file
#log4j.rootLogger=ERROR,ROLLING_FILE
log4j.logger.cn.smbms.dao=debug
log4j.logger.com.ibatis=debug
log4j.logger.com.ibatis.common.jdbc.SimpleDataSource=debug
log4j.logger.com.ibatis.common.jdbc.ScriptRunner=debug
log4j.logger.com.ibatis.sqlmap.engine.impl.SqlMapClientDelegate=debug
log4j.logger.java.sql.Connection=debug
log4j.logger.java.sql.Statement=debug
log4j.logger.java.sql.PreparedStatement=debug
log4j.logger.java.sql.ResultSet=debug
log4j.logger.org.tuckey.web.filters.urlrewrite.UrlRewriteFilter=debug
######################################################################################
# Console Appender \u65e5\u5fd7\u5728\u63a7\u5236\u8f93\u51fa\u914d\u7f6e
######################################################################################
log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender
log4j.appender.Threshold=error
log4j.appender.CONSOLE.Target=System.out
log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout
log4j.appender.CONSOLE.layout.ConversionPattern= [%p] %d %c - %m%n
######################################################################################
# DailyRolling File \u6bcf\u5929\u4ea7\u751f\u4e00\u4e2a\u65e5\u5fd7\u6587\u4ef6\uff0c\u6587\u4ef6\u540d\u683c\u5f0f:log2009-09-11
######################################################################################
log4j.appender.file=org.apache.log4j.DailyRollingFileAppender
log4j.appender.file.DatePattern=yyyy-MM-dd
log4j.appender.file.File=log.log
log4j.appender.file.Append=true
log4j.appender.file.Threshold=error
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%d{yyyy-M-d HH:mm:ss}%x[%5p](%F:%L) %m%n
log4j.logger.com.opensymphony.xwork2=error
7、配置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">
编写配置文件
<!--通过mybatis配置文件实现与数据库的连接-->
<configuration>
<!--引入database.properties文件-->
<properties resource="database.properties"/>
<!--配置mybatis的log实现log4j-->
<settings>
<setting name="logImpl" value="LOG4J"/>
</settings>
<environments default="development">
<environment id="development">
<!--配置事务管理,采用jdbc的事务管理-->
<transactionManager type="JDBC"></transactionManager>
<!--使用mybatis自带的数据源pooled类型-->
<dataSource type="POOLED">
<property name="driver" value="${driver1}"/>
<property name="url" value="${url8}"/>
<property name="username" value="${user}"/>
<property name="password" value="${password}"/>
</dataSource>
</environment>
</environments>
<!--将mapper文件加入到配置文件中,后续补充-->
<mappers>
<mapper resource=""/>
</mappers>
</configuration>
8、补充src内的java类
编写实体类
建议:
实体类的类名可以不和表明一致,但是属性名建议和表中的字段保持一致。
同时,设计表字段时,要遵循驼峰命名法
package cn.smbms.pojo;
import java.util.Date;
public class User {
private Integer id; //id
private String userCode; //用户编码
private String userName; //用户名称
private String userPassword; //用户密码
private Integer gender; //性别
private Date birthday; //出生日期
private String phone; //电话
private String address; //地址
private Integer userRole; //用户角色
private Integer createdBy; //创建者
private Date creationDate; //创建时间
private Integer modifyBy; //更新者
private Date modifyDate; //更新时间
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getUserCode() {
return userCode;
}
public void setUserCode(String userCode) {
this.userCode = userCode;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getUserPassword() {
return userPassword;
}
public void setUserPassword(String userPassword) {
this.userPassword = userPassword;
}
public Integer getGender() {
return gender;
}
public void setGender(Integer gender) {
this.gender = gender;
}
public Date getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public Integer getUserRole() {
return userRole;
}
public void setUserRole(Integer userRole) {
this.userRole = userRole;
}
public Integer getCreatedBy() {
return createdBy;
}
public void setCreatedBy(Integer createdBy) {
this.createdBy = createdBy;
}
public Date getCreationDate() {
return creationDate;
}
public void setCreationDate(Date creationDate) {
this.creationDate = creationDate;
}
public Integer getModifyBy() {
return modifyBy;
}
public void setModifyBy(Integer modifyBy) {
this.modifyBy = modifyBy;
}
public Date getModifyDate() {
return modifyDate;
}
public void setModifyDate(Date modifyDate) {
this.modifyDate = modifyDate;
}
}
编写dao层中接口
package cn.smbms.dao.user;
public interface UserMapper {
//需求是:统计用户表中的记录数
// select count(1) as count from smbms_user ;
public int count();
}
创建对应UserMapper的mapper文件
注意点: 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">
9 编写sql语句
<!--namespace 命名空间 :填写 对应此xml'文件的接口名-->
<mapper namespace="cn.smbms.dao.user.UserMapper">
<!--进行查询操作-->
<!--id 填写接口中的方法名 resultType 填写 返回类型-->
<select id="count" resultType="int">
select count(1) as count from smbms_user
</select>
</mapper>
10 添加mapper文件路径到mybatis-config.xml中
通过右击UserMapper.xml,选择copy relative path,复制粘贴到mybatis-config.xml中
11 编写测试类
创建测试文件夹
mark测试文件夹
创建java类文件
编写具体测试方法
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.Logger;
import org.junit.Test;
import java.io.IOException;
import java.io.InputStream;
public class UserMapperTest {
//创建日志对象
Logger logger=Logger.getLogger(UserMapperTest.class);
//测试方法
@Test
public void test(){
SqlSession sqlSession=null;
try {
//1 获得mybatis-config.xml文件的输入流
InputStream inputStream= Resources.getResourceAsStream("mybatis-config.xml");
//2 创建SqlSessionFactory对象,读取上述流
SqlSessionFactory sqlSessionFactory=new SqlSessionFactoryBuilder().build(inputStream);
//3 创建SqlSession对象(Connection)
sqlSession=sqlSessionFactory.openSession();
//4 通过sqlSession对象对mapper文件对应的接口进行方法的读取 全路径+方法名
int count = sqlSession.selectOne("cn.smbms.dao.user.UserMapper.count");
logger.debug("UserMapperTest test count:"+count);
} catch (IOException e) {
e.printStackTrace();
}finally {
sqlSession.close();
}
}
}
12 测试运行
作业
编写程序,查询订单表中的记录数,并能够成功运行。