1. 开发环境(部分)
基于原有环境
- MyBatis 3.3.0
- MyBatis-Spring 1.2.3
- MySQL 5.5.47
2. 文件结构
2.1 项目目录
文件结构.png新增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相关依赖-->
<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数据库链接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;
/**
* 数据库的增删查改等操作在这里执行
* MyBatis MapperScan 会扫描到这里,完成sql语句与相关操作语句的映射
* 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();
// 数据库连接配置
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的相关信息
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 测试数据表准备
测试表.png4.2 运行 ‘ 程序 ’
测试成功.png小结
本例选用的数据持久层框架是 MyBatis,各位可以根据自己的情况选择合适的框架。配置过程大同小异。
注意maven 依赖的引入,以及相关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/
网友评论