Spring-boot、Spring-jdbc 、Spring-mvc

Appliction.java

package hello;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.datasource.SimpleDriverDataSource;

@ComponentScan
@EnableAutoConfiguration
public class Application {
	private static JdbcTemplate jdbcTemplate;

	//查询数据
	public static List<Greeting> getData(String name) {
		System.out.println("select * from Greetings where content like %"+name+"%;");
		List<Greeting> results = jdbcTemplate.query(
				"select * from Greetings where content like ? ",
				new Object[] { "%"+name+"%" }, new RowMapper<Greeting>() {
					@Override
					public Greeting mapRow(ResultSet rs, int rowNum)
							throws SQLException {
						return new Greeting(rs.getLong("id"), rs
								.getString("content"));
					}
				});
		System.out.println("size:"+results.size());
		return results;
	}

	//建立表,并插入数据
	private static void initTable() {
		System.out.println("Creating tables");
		jdbcTemplate.execute("create table Greetings("
				+ "id serial, content varchar(255))");
		String[] names = "John Woo;Jeff Dean;John Woo;Josh Long".split(";");
		for (String name : names) {
			System.out.printf("Inserting Greeting record for %s\n", name);
			jdbcTemplate.update("INSERT INTO Greetings(content) values(?)",
					name);
		}
	}
	
	//初始化数据库链接,得到jdbcTemplate
	private static void connectionDB() {
		SimpleDriverDataSource dataSource = new SimpleDriverDataSource();
		dataSource.setDriverClass(com.mysql.jdbc.Driver.class);
		dataSource.setUsername("root");
		dataSource.setUrl("jdbc:mysql://localhost:3306/test");
		dataSource.setPassword("root");
		jdbcTemplate = new JdbcTemplate(dataSource);
	}

	
	public static void main(String args[]) {
		SpringApplication.run(Application.class, args);
		connectionDB();
//		 initTable();
		 List<Greeting> results = getData("John");
		 for (Greeting Greeting : results) {
				System.out.println(Greeting);
			}
	}
}

GreetingController

package hello;

import java.util.List;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class GreetingController {


    @RequestMapping("/greeting")
    public List<Greeting> greeting(@RequestParam(value="name", required=false, defaultValue="John Woo") String name) {
       System.out.println("name"+name);
        List<Greeting> res = Application.getData(name);
    	return res;
    }
}

模型Greeting

package hello;

public class Greeting {

    private final long id;
    private final String content;

    public Greeting(long id, String content) {
        this.id = id;
        this.content = content;
    }

    public long getId() {
        return id;
    }

    public String getContent() {
        return content;
    }
}

maven配置

<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/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>testspringmvc</groupId>
  <artifactId>testspringmvc</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>jar</packaging>

  <name>testspringmvc</name>
  <url>http://maven.apache.org</url>


  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
      <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
      <dependency>
      	<groupId>org.springframework</groupId>
      	<artifactId>spring-jdbc</artifactId>
      	<version>4.0.6.RELEASE</version>
      </dependency>
      <dependency>
      	<groupId>mysql</groupId>
      	<artifactId>mysql-connector-java</artifactId>
      	<version>5.1.21</version>
      </dependency>
  </dependencies>
  <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.1.3.RELEASE</version>
    </parent>


    <properties>
      <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <start-class>hello.Application</start-class>
    </properties>

    <build>
        <plugins>
            <plugin> 
                <artifactId>maven-compiler-plugin</artifactId> 
                <version>2.3.2</version> 
            </plugin>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

    <repositories>
        <repository>
            <id>spring-releases</id>
            <url>http://repo.spring.io/libs-release</url>
        </repository>
    </repositories>
    <pluginRepositories>
        <pluginRepository>
            <id>spring-releases</id>
            <url>http://repo.spring.io/libs-release</url>
        </pluginRepository>
    </pluginRepositories>
  
</project>

演示:

    

源码下载地址:http://download.csdn.net/detail/chenxiruanhai/7685107


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值