框架编程技术笔记**
环境搭建
编写核心XML文件——mybatis-config.xml文件
在java resources下创建一个source folder,命名为resources
在resources下需要创建三个文件分别为:database.properties/log4j.properties/mybatis-config.xml
datebase.properties的具体代码为
driver=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/smbms?useUnicode=true&characterEncoding=utf-8
user=root
password=root
log4j.properties的具体代码为
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
mybatis-config的代码为
<?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>
<!-- POOLED:mybatis自带的数据源,JNDI:基于tomcat的数据源 -->
<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>
<!-- 将mapper文件加入到配置文件中 -->
<mappers>
<mapper resource="cn/smbms/dao/provider/ProviderMapper.xml"/>
</mappers>
</configuration>
编写src下的pojo和Mapper
src下右键——>new package,取名cn.smbms.dao.provider(以超市管理系统为例),在其下新建一个ProviderMapper.xml的文件
ProviderMapper.xml的作用是书写sql语言,以便于对数据库进行操作,ProviderMapper.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">
<mapper namespace="cn.smbms.dao.provider.ProviderMapper">
<!-- 查询供应商表记录数 -->
<select id="count" resultType="int">
select count(1) as count from smbms_provider
</select>
</mapper>
src下右键——>new package,取名cn.smbms.pojo,其作用是创建provider对象。具体的代码如下:
package cn.smbms.pojo;
import java.util.*;
public class Provider {
private Integer id;//id
private String proCode;//供货商编码
private String proName;//供货商名
private String proDesc;
private String proContact;
private String proPhone;
private String proAddress;
private String proFax;
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 getProCode() {
return proCode;
}
public void setProCode(String proCode) {
this.proCode = proCode;
}
public String getProName() {
return proName;
}
public void setProName(String proName) {
this.proName = proName;
}
public String getProDesc() {
return proDesc;
}
public void setProDesc(String proDesc) {
this.proDesc = proDesc;
}
public String getProContact() {
return proContact;
}
public void setProContact(String proContact) {
this.proContact = proContact;
}
public String getProPhone() {
return proPhone;
}
public void setProPhone(String proPhone) {
this.proPhone = proPhone;
}
public String getProAddress() {
return proAddress;
}
public void setProAddress(String proAddress) {
this.proAddress = proAddress;
}
public String getProFax() {
return proFax;
}
public void setProFax(String proFax) {
this.proFax = proFax;
}
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;
}
}
编写test文件夹
编写测试类ProviderMapperTest.java
package cn.smbms.dao.provider;
import static org.junit.Assert.*;
import java.io.IOException;
import java.io.InputStream;
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.Before;
import org.junit.Test;
public class ProviderMapperTest {
private Logger logger = Logger.getLogger(ProviderMapperTest.class);
@Before
public void setUp() throws Exception {
}
@Test
public void test() {
String resource = "mybatis-config.xml";
int count = 0;
SqlSession sqlSession = null;
try {
//1 获取mybatis-config.xml的输入流
InputStream is = Resources.getResourceAsStream(resource);
//2 创建SqlSessionFactory对象,完成对配置文件的读取
SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(is);
//3 创建sqlSession
sqlSession = factory.openSession();
//4 调用mapper文件来对数据进行操作,必须先把mapper文件引入到mybatis-config.xml中
count = sqlSession.selectOne("cn.smbms.dao.provider.ProviderMapper.count");
logger.debug("ProviderMapperTest count---> " + count);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
sqlSession.close();
}
}
}
测试运行
查看输出结果