1. 開發(fā)環(huán)境(部分)
基于原有環(huán)境
- MyBatis 3.3.0
- MyBatis-Spring 1.2.3
- MySQL 5.5.47
2. 文件結(jié)構(gòu)
2.1 項目目錄
新增User(entity),UserDao(Repository)剖笙,WebContextConfig(beans.xml)
對hello.html、HelloController稍作修改
2.2 pom.xml 添加部分依賴
<!--僅包含部分台诗,詳見源碼pom文件-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<!--mybatis相關(guān)依賴-->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.3.0</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>1.2.3</version>
</dependency>
<!-- 導入Mysql數(shù)據(jù)庫鏈接jar包 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.37</version>
</dependency>
<!-- 連接池包 -->
<dependency>
<groupId>commons-dbcp</groupId>
<artifactId>commons-dbcp</artifactId>
<version>1.4</version>
</dependency>
<dependency>
<groupId>commons-pool</groupId>
<artifactId>commons-pool</artifactId>
<version>1.6</version>
</dependency>
3. 代碼編寫
3.1 User.java
package com.practice;
/**
* Created by SXY on 2016/1/26.
*/
public class User {
private String name;
private int age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
3.2 UserDao.java
package com.practice;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Repository;
/**
* 數(shù)據(jù)庫的增刪查改等操作在這里執(zhí)行
* MyBatis MapperScan 會掃描到這里孙蒙,完成sql語句與相關(guān)操作語句的映射
* Created by SXY on 2016/1/26.
*/
@Repository
public interface UserDao {
@Select("select * from user_info where name=#{name} limit 1")
User getOneUser(String name);
}
3.3 WebContextConfig.java
package com.practice;
import org.apache.commons.dbcp.BasicDataSource;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import javax.sql.DataSource;
/**
* 類似于spring-context.xml或者beans.xml
* MapperScan用于掃描Mapper映射接口,比如本例中的 UserDao
* MapperScan几苍、ComponentScan具體的使用方法 跟之前的ComponentScan 完全相同 此處便不再贅述
* Created by SXY on 2016/1/26.
*/
@Configuration
@MapperScan(basePackages = "com.practice")
@ComponentScan(basePackages = "com.practice")
public class WebContextConfig {
@Bean
public DataSource getDataSource() {
BasicDataSource dataSource = new BasicDataSource();
// 數(shù)據(jù)庫連接配置
dataSource.setDriverClassName("com.mysql.jdbc.Driver");
dataSource.setUrl("jdbc:mysql://localhost:3306/test");
dataSource.setUsername("root");
dataSource.setPassword("root");
return dataSource;
}
// 事務管理
@Bean
public DataSourceTransactionManager transactionManager() {
return new DataSourceTransactionManager(getDataSource());
}
@Bean(name = "sqlSessionFactory")
public SqlSessionFactory sqlSessionFactory() throws Exception {
SqlSessionFactoryBean sqlSessionFactory = new SqlSessionFactoryBean();
sqlSessionFactory.setDataSource(getDataSource());
return sqlSessionFactory.getObject();
}
}
3.4 修改 HelloController.java
package com.practice;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
/**
* Created by SXY on 2016/1/18.
*/
// 聲明為controller控制器,捕獲請求并處理請求
@Controller
public class HelloController {
// 注入UserDao
@Autowired
private UserDao userDao;
// 捕獲 /hello 請求陈哑,利用hello(Model model) 處理請求妻坝,并返回‘hello’,交給Thymeleaf 處理
// hello 對應html文件名惊窖。model 作為信息的載體刽宪,封裝各類變量、對象
@RequestMapping("/hello")
public String hello(Model model) {
// 獲取XiaoMing的相關(guān)信息
User user = userDao.getOneUser("XiaoMing");
model.addAttribute("name", user.getName());
model.addAttribute("age", user.getAge());
return "hello";
}
}
3.6 修改 hello.html
<!-- 該文件要放置在 resources/templates/下 -->
<!DOCTYPE html>
<!-- 添加 Thymeleaf 命名空間-->
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8"/>
<title>Spring4 MVC</title>
</head>
<body>
<p th:text="'Hi,My name is ' + ${name} + ','+'愛沐 '+${age}+' 葉子歐德'" />
</body>
</html>
4. 測試
4.1 MySQL 測試數(shù)據(jù)表準備
4.2 運行 ‘ 程序 ’
小結(jié)
本例選用的數(shù)據(jù)持久層框架是 MyBatis界酒,各位可以根據(jù)自己的情況選擇合適的框架圣拄。配置過程大同小異。
注意maven 依賴的引入毁欣,以及相關(guān)bean的添加庇谆。Spring 官方DataAccess文檔
http://docs.spring.io/spring/docs/current/spring-framework-reference/htmlsingle/#spring-data-tier
源碼地址:http://git.oschina.net/tobe/Spring4MVC/tree/connect_mysql/