準(zhǔn)備好好學(xué)習(xí)一下SSM框架,首先從最簡單的MyBatis開始荆姆。主要參考《Java EE互聯(lián)網(wǎng)輕量型框架整合開發(fā)》和《Spring MVC+MyBatis開發(fā) 從入門到項(xiàng)目實(shí)戰(zhàn)》這兩本書执桌。本文也可以認(rèn)為是這兩本書的讀書筆記桅狠,
MyBatis特點(diǎn)
MyBatis是采用配置文件動態(tài)管理SQL語句奸笤,并含有輸入映射痕檬、輸出映射機(jī)制以及數(shù)據(jù)庫連接池配置的持久層框架。
MyBatis的核心組組件分成以下4個部分:
- SqlSessionFactoryBuilder(構(gòu)造器): 它會根據(jù)配置或者代碼來生成SqlSessionFactory,采用的是分步構(gòu)建的Builder模式歹苦。
- SqlSessionFactory(工廠接口):依賴它來生成SqlSession青伤。
- SqlSession(會話): 一個既可以發(fā)送SQL執(zhí)行返回結(jié)果,又可以獲取Mapper的接口殴瘦。
- SQL Mapper(映射器): MyBatis新設(shè)計(jì)存在的組件狠角,它由一個Java接口和XML文件構(gòu)成,需要給出對應(yīng)的的SQL和映射規(guī)則蚪腋。
下面開始搭建項(xiàng)目丰歌。
準(zhǔn)備環(huán)境
準(zhǔn)備數(shù)據(jù)庫
打開MySql數(shù)據(jù)庫,創(chuàng)建一個簡單的數(shù)據(jù)表并插入幾條數(shù)據(jù)屉凯。
- 創(chuàng)建數(shù)據(jù)庫mybatis_demo立帖。
create database `mybatis_demo`;
use `mybatis_demo`;
- 創(chuàng)建數(shù)據(jù)表t_users。
drop table if exists `t_users`;
create table `t_users` (
`id` INT(11) NOT NULL AUTO_INCREMENT,
`username` VARCHAR(120) COLLATE utf8_bin DEFAULT NULL,
`password` VARCHAR(50) COLLATE utf8_bin DEFAULT NULL,
`gender` VARCHAR(5) COLLATE utf8_bin DEFAULT NULL,
`email` VARCHAR(100) COLLATE utf8_bin DEFAULT NULL,
`province` VARCHAR(50) COLLATE utf8_bin DEFAULT NULL,
`city` VARCHAR(120) COLLATE utf8_bin DEFAULT NULL,
`birthday` DATE DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=INNODB DEFAULT CHARSET=utf8 COLLATE=utf8_bin;
- 插入幾條數(shù)據(jù)悠砚。
insert into `t_users` (`id`, `username`, `password`, `gender`, `email`, `province`, `city`, `birthday`)
values
(1, '張三','111','男','111@126.com','河北省','衡水市','1990-01-01'),
(2, '李四','222','男','222@126.com','河南省','洛陽市','1992-08-08'),
(3, '王五','333','男','333@126.com','湖南省','長沙市','1978-03-15'),
(4, '趙麗','444','女','444@126.com','湖北省','武漢市','1999-12-01');
一個需要注意的地方是引號和反引號的使用晓勇。在插入列的時候必須使用引號,其它地方大部分使用反引號灌旧。
搭建工程
使用Eclipse創(chuàng)建一個Maven Web項(xiàng)目绑咱。打開pom.xml,添加依賴包。分別是MyBatis依賴包枢泰、MySql數(shù)據(jù)庫連接jar包 和日志包描融。
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.wyk</groupId>
<artifactId>mybatisDemo</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>mybatisDemo Maven Webapp</name>
<url>http://maven.apache.org</url>
<properties>
<!-- MyBatis版本號 -->
<mybatis.version>3.2.6</mybatis.version>
<log4j.version>1.2.17</log4j.version>
</properties>
<dependencies>
<!-- 導(dǎo)入Mysql數(shù)據(jù)庫連接jar包 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.30</version>
</dependency>
<!-- MyBatis核心包 -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>${mybatis.version}</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>${log4j.version}</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<finalName>mybatisDemo</finalName>
</build>
</project>
進(jìn)行開發(fā)
下面編寫小項(xiàng)目的代碼并添加配置文件。
添加實(shí)體類
創(chuàng)建com.wyk.mybatisDemo.pojo包并在其中添加Java實(shí)體類User宗苍,用來和數(shù)據(jù)表相對應(yīng)稼稿。
package com.wyk.mybatisDemo.pojo;
import java.io.Serializable;
import java.util.Date;
public class User implements Serializable {
private int id;
private String username;
private String password;
private String gender;
private String email;
private String province;
private String city;
private Date birthday;
public User() {}
public User(int id, String username, String password, String gender,
String email, String province, String city, Date birthday) {
super();
this.id = id;
this.username = username;
this.password = password;
this.gender = gender;
this.email = email;
this.province = province;
this.city = city;
this.birthday = birthday;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getProvince() {
return province;
}
public void setProvince(String province) {
this.province = province;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public Date getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
}
在該實(shí)體中,創(chuàng)建了User的所有屬性信息和他們的get讳窟、set方法让歼,并且創(chuàng)建了一個無參構(gòu)造函數(shù)和一個有參構(gòu)造函數(shù)。
配置日志
在resources目錄下創(chuàng)建文件log4j.properties丽啡,為log4j日志輸出環(huán)境配置參數(shù)谋右。
#Global logging configuration
#
log4j.rootLogger=DEBUG, stdout
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern==%d{yyyy-MM-dd HH:mm:ss} [%c]-[%p] %m%n
配置數(shù)據(jù)庫連接池
在resources下創(chuàng)建文件SqlMapConfig.xml,添加數(shù)據(jù)庫連接相關(guān)的配置补箍。
<?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>
<settings>
<!-- 日志 -->
<setting name="logImpl" value="LOG4J" />
</settings>
<environments default="development">
<environment id="development">
<!-- 使用JDBC事務(wù)管理 -->
<transactionManager type="JDBC"></transactionManager>
<!-- 數(shù)據(jù)庫連接池 -->
<dataSource type="POOLED">
<property name="driver" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/mybatis_demo?characterEncoding=utf-8"/>
<property name="username" value="root" />
<property name="password" value="123" />
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource="com/wyk/mybatisDemo/mapper/userMapper.xml" />
</mappers>
</configuration>
注意開頭的DTD不能寫錯梧税。
配置SQL映射
前面注意到拂蝎,SqlMapConfig.xml的mappers標(biāo)簽下已經(jīng)添加了一個引用,這就是我們要創(chuàng)建的文件。創(chuàng)建com.wyk.mybatisDemo.mapper包并添加xml文件usserMapper.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="com.wyk.mybatisDemo.tests" >
<select id="findUserById" parameterType="int" resultType="com.wyk.mybatisDemo.pojo.User">
SELECT * FROM T_USERS WHERE id=#{id}
</select>
</mapper>
創(chuàng)建數(shù)據(jù)庫交互類
創(chuàng)建包c(diǎn)om.wyk.mybatisDemo.datasource,并在其中創(chuàng)建一個可以獲取sqlSession的類DataConnection幌绍,采用了單例模式邑贴。
package com.wyk.mybatisDemo.datasource;
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;
public class DataConnection {
private final static Class<DataConnection> LOCK = DataConnection.class;
private static String resource = "SqlMapConfig.xml";
private static SqlSessionFactory sqlSessionFactory = null;
private DataConnection() {}
public static SqlSessionFactory getSqlSessionFactory() {
//加鎖恬吕,防止多次實(shí)例化
synchronized(LOCK) {
if(sqlSessionFactory != null) {
return sqlSessionFactory;
}
InputStream inputStream;
try {
inputStream = Resources.getResourceAsStream(resource);
sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
} catch (IOException e) {
e.printStackTrace();
return null;
}
return sqlSessionFactory;
}
}
public static SqlSession openSqlSession() {
if(sqlSessionFactory == null) {
getSqlSessionFactory();
}
return sqlSessionFactory.openSession();
}
}
其中類的構(gòu)造參數(shù)加入了private關(guān)鍵字,使其他代碼無法創(chuàng)建它。
進(jìn)行測試
在com.wyk.mybatisDemo.tests包下添加JUnit測試用例MyBatisTest拇泣。
package com.wyk.mybatisDemo.tests;
import java.io.IOException;
import java.text.SimpleDateFormat;
import org.apache.ibatis.session.SqlSession;
import org.junit.Test;
import com.wyk.mybatisDemo.datasource.DataConnection;
import com.wyk.mybatisDemo.pojo.User;
import junit.framework.TestCase;
public class MyBatisTest extends TestCase {
@Test
public void TestSelect() throws IOException {
SqlSession sqlSession = DataConnection.openSqlSession();
//statement的格是當(dāng)前類名+SQL語句的ID
User user = sqlSession.selectOne("com.wyk.mybatisDemo.tests.findUserById", 1);
System.out.println("姓名:" + user.getUsername());
System.out.println("性別:" + user.getGender());
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
System.out.println("生日:" + sdf.format(user.getBirthday()));
System.out.println("所在地:" + user.getProvince() + user.getCity());
sqlSession.close();
}
}
右鍵該類選擇Run As->Run Configuration,在Test中選擇要測試的方法噪叙,并修改Test Runner為JUint3。
運(yùn)行測試用例霉翔,查看測試結(jié)果睁蕾。
補(bǔ)充
補(bǔ)充2個小知識點(diǎn)。
"#{}"和"${}"的區(qū)別
"#{}"是占位符债朵,類似傳統(tǒng)JDBC里面的"?"; "${"}將傳入的數(shù)據(jù)直接顯示生成在sql之中子眶。
"#{}"可以防止SQL注入,原則上是可以使用"#{}"的地方全用"#{}"序芦。
一些地方必須使用"${}":字符需要進(jìn)行拼接的時候(比如兩側(cè)有%)壹店;排序使用order byd動態(tài)參數(shù);傳入數(shù)據(jù)庫對象(例如表名)芝加。
PS:前面寫道字符需要進(jìn)行拼接的時候必須使用"${}"。后面發(fā)現(xiàn)其實(shí)使用concat函數(shù)可以達(dá)到同樣的效果射窒,特此記錄藏杖。
下例中的兩個SQL效果是一樣的:
<select id="findUserByUsername" resultType="user" >
SELECT * FROM T_USERS WHERE username LIKE '%${value}%'
</select>
<select id="findUserByUsername2" resultType="user" >
SELECT * FROM T_USERS WHERE username LIKE CONCAT('%', #{value}, '%')
</select>
"selectOne"和"selectList"
這個比較簡單,前者返回一條記錄脉顿,后者返回多條記錄蝌麸。