Mybatis 学习-1

本次MyBatis基础实例教程主要讲述MyBatis在项目中的配置方法,实体对象的关系映射、关联关系,以及如何实现一个BaseDao的功能

实例数据库的表结构

Mybatis 学习-1
CREATE TABLE `blog_user` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`user_name` varchar(45) NOT NULL,
`user_pass` varchar(45) NOT NULL,
`nick_name` varchar(45) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=utf8; CREATE TABLE `blog_article` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`art_title` varchar(100) NOT NULL,
`art_content` text NOT NULL,
`art_pubtime` datetime DEFAULT NULL,
`pub_user_id` varchar(45) DEFAULT NULL,
`cate_id` int(11) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=6 DEFAULT CHARSET=utf8; CREATE TABLE `blog_category` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`cate_name` varchar(45) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8;
Mybatis 学习-1

###整个MyBatis 应用使用Maven进行管理

1.首先导包  在pom文件中添加相关包的引入

Mybatis 学习-1
<!-- MyBatis框架 -->
<dependency>
  <groupId>org.mybatis</groupId>
  <artifactId>mybatis</artifactId>
  <version>3.2.8</version>
</dependency> <!-- MySql数据库驱动 -->
<dependency>
  <groupId>mysql</groupId>
  <artifactId>mysql-connector-java</artifactId>
  <version>5.1.34</version>
</dependency> <dependency>
  <groupId>log4j</groupId>
  <artifactId>log4j</artifactId>
  <version>1.2.9</version>
</dependency> <!-- log4j相关包 在控制台输出日志 -->
<dependency>
  <groupId>org.slf4j</groupId>
  <artifactId>slf4j-api</artifactId>
  <version>1.6.1</version>
</dependency>
<dependency>
   <groupId>org.slf4j</groupId>
   <artifactId>slf4j-log4j12</artifactId>
   <version>1.6.1</version>
</dependency>
<dependency>
  <groupId>org.slf4j</groupId>
  <artifactId>slf4j-nop</artifactId>
  <version>1.6.1</version>
</dependency>
Mybatis 学习-1

2.在项目中添加log4j.properties文件,输出MyBaits运行日志

Mybatis 学习-1
log4j.rootLogger=debug,stdout,logfile

log4j.appender.stdout=org.apache.log4j.ConsoleAppender
#log4j.appender.stdout.Target=System.err
log4j.appender.stdout.layout=org.apache.log4j.SimpleLayout log4j.appender.logfile=org.apache.log4j.FileAppender
log4j.appender.logfile.File=jbit.log
log4j.appender.logfile.layout=org.apache.log4j.PatternLayout
log4j.appender.logfile.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %F %p %m%n 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
Mybatis 学习-1

3.指定MyBaits核心配置文件(mybatis.cfg.xml)

Mybatis 学习-1
<?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>
  <environments default="news_res">
<environment id="news_res">
<transactionManager type="JDBC"/>
<dataSource type="POOLED">
   <property name="driver" value="com.mysql.jdbc.Driver"/>
   <property name="url" value="jdbc:mysql://localhost:3306/newsdb" />
   <property name="username" value="root"/>
   <property name="password" value="root"/>
</dataSource>
</environment>
</environments>
</configuration>
Mybatis 学习-1

4.完成基本功能测试

Mybatis 学习-1
public class SessionOpenTest {

    @Test
public void test() {
boolean openFlag=false;
try {
  Reader reader = Resources.getResourceAsReader("mybatis.cfg.xml");
  SqlSessionFactory ssf = new
       SqlSessionFactoryBuilder().build(reader);
  SqlSession session = ssf.openSession();
if(!session.getConnection().isClosed()){
openFlag = true;
}
} catch (IOException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
Assert.assertTrue(openFlag);
}
}
 

本次MyBatis基础实例教程主要讲述MyBatis在项目中的配置方法,实体对象的关系映射、关联关系,以及如何实现一个BaseDao的功能

实例数据库的表结构

Mybatis 学习-1
CREATE TABLE `blog_user` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`user_name` varchar(45) NOT NULL,
`user_pass` varchar(45) NOT NULL,
`nick_name` varchar(45) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=utf8; CREATE TABLE `blog_article` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`art_title` varchar(100) NOT NULL,
`art_content` text NOT NULL,
`art_pubtime` datetime DEFAULT NULL,
`pub_user_id` varchar(45) DEFAULT NULL,
`cate_id` int(11) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=6 DEFAULT CHARSET=utf8; CREATE TABLE `blog_category` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`cate_name` varchar(45) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8;
Mybatis 学习-1

###整个MyBatis 应用使用Maven进行管理

1.首先导包  在pom文件中添加相关包的引入

Mybatis 学习-1
<!-- MyBatis框架 -->
<dependency>
  <groupId>org.mybatis</groupId>
  <artifactId>mybatis</artifactId>
  <version>3.2.8</version>
</dependency> <!-- MySql数据库驱动 -->
<dependency>
  <groupId>mysql</groupId>
  <artifactId>mysql-connector-java</artifactId>
  <version>5.1.34</version>
</dependency> <dependency>
  <groupId>log4j</groupId>
  <artifactId>log4j</artifactId>
  <version>1.2.9</version>
</dependency> <!-- log4j相关包 在控制台输出日志 -->
<dependency>
  <groupId>org.slf4j</groupId>
  <artifactId>slf4j-api</artifactId>
  <version>1.6.1</version>
</dependency>
<dependency>
   <groupId>org.slf4j</groupId>
   <artifactId>slf4j-log4j12</artifactId>
   <version>1.6.1</version>
</dependency>
<dependency>
  <groupId>org.slf4j</groupId>
  <artifactId>slf4j-nop</artifactId>
  <version>1.6.1</version>
</dependency>
Mybatis 学习-1

2.在项目中添加log4j.properties文件,输出MyBaits运行日志

Mybatis 学习-1
log4j.rootLogger=debug,stdout,logfile

log4j.appender.stdout=org.apache.log4j.ConsoleAppender
#log4j.appender.stdout.Target=System.err
log4j.appender.stdout.layout=org.apache.log4j.SimpleLayout log4j.appender.logfile=org.apache.log4j.FileAppender
log4j.appender.logfile.File=jbit.log
log4j.appender.logfile.layout=org.apache.log4j.PatternLayout
log4j.appender.logfile.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %F %p %m%n 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
Mybatis 学习-1

3.指定MyBaits核心配置文件

Mybatis 学习-1
<?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>
  <environments default="news_res">
<environment id="news_res">
<transactionManager type="JDBC"/>
<dataSource type="POOLED">
   <property name="driver" value="com.mysql.jdbc.Driver"/>
   <property name="url" value="jdbc:mysql://localhost:3306/newsdb" />
   <property name="username" value="root"/>
   <property name="password" value="root"/>
</dataSource>
</environment>
</environments>
</configuration>
Mybatis 学习-1

4.完成基本功能测试

Mybatis 学习-1
public class SessionOpenTest {

    @Test
public void test() {
boolean openFlag=false;
try {
  Reader reader = Resources.getResourceAsReader("mybatis.cfg.xml");
  SqlSessionFactory ssf = new
       SqlSessionFactoryBuilder().build(reader);
  SqlSession session = ssf.openSession();
if(!session.getConnection().isClosed()){
openFlag = true;
}
} catch (IOException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
Assert.assertTrue(openFlag);
}
}
 
上一篇:cron语法


下一篇:Struts2使用Interceptor实现权限控制的应用实例详解