spring4 mysql_【Spring4 MVC】(三)-连接MySQL数据库

1. 开发环境(部分)

基于原有环境

MyBatis 3.3.0

MyBatis-Spring 1.2.3

MySQL 5.5.47

2. 文件结构

2.1 项目目录

新增User(entity),UserDao(Repository),WebContextConfig(beans.xml)

对hello.html、HelloController稍作修改

abc21216bdf5?nomobile

文件结构.png

2.2 pom.xml 添加部分依赖

org.springframework.boot

spring-boot-starter-thymeleaf

org.springframework.boot

spring-boot-starter-jdbc

org.mybatis

mybatis

3.3.0

org.mybatis

mybatis-spring

1.2.3

mysql

mysql-connector-java

5.1.37

commons-dbcp

commons-dbcp

1.4

commons-pool

commons-pool

1.6

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

Spring4 MVC

4. 测试

4.1 MySQL 测试数据表准备

abc21216bdf5?nomobile

测试表.png

4.2 运行 ‘ 程序 ’

abc21216bdf5?nomobile

测试成功.png

小结

本例选用的数据持久层框架是 MyBatis,各位可以根据自己的情况选择合适的框架。配置过程大同小异。

注意maven 依赖的引入,以及相关bean的添加。Spring 官方DataAccess文档

http://docs.spring.io/spring/docs/current/spring-framework-reference/htmlsingle/#spring-data-tier

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值