Coin163

首页 > [置顶] Spring-boot、Spring-jdbc 、Spring-mvc

[置顶] Spring-boot、Spring-jdbc 、Spring-mvc

相关标签: spring-mvc spring-boot

2020腾讯云双十一活动,全年最低!!!(领取3500元代金券),
地址https://cloud.tencent.com/act/cps/redirect?redirect=1073

2020阿里云最低价产品入口,含代金券(新老用户有优惠),
入口地址https://www.aliyun.com/minisite/goods

相关推荐:Spring Boot入门

今天面试问到了这个框架,补一补!!积累积累 参考:http://blog.csdn.net/xiaoyu411502/article/details/47864969#comments http://www.zhihu.com/question/39483566/answer/92163407 http://www.infoq.com/cn/articles/microframeworks1-spring-boot/ http:/

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;
	}

	//建立表,

相关推荐:Spring Boot——2分钟构建spring web mvc REST风格HelloWorld

转自:http://jinnianshilongnian.iteye.com/blog/1997192 Spring Boot使我们更容易去创建基于Spring的独立和产品级的可以”即时运行“的应用和服务。支持约定大于配置,目的是尽可能快地构建和运行Spring应用。   之前我们创建基于Spring的项目需要考虑添加

并插入数据 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


原文

Appliction.java package hello;import java.sql.ResultSet;import java.sql.SQLException;import java.util.List;import org.springframework.boot.SpringApplication;import org.sprin

------分隔线----------------------------