springboot jdbc(mysql)

pom.xml

<?xml version=”1.0” encoding=”UTF-8”?>


4.0.0
com.example
springboot
0.0.2

org.springframework.boot
spring-boot-starter-parent
1.3.0.BUILD-SNAPSHOT




org.springframework.boot
spring-boot-starter-web


org.springframework.boot
spring-boot-starter-data-jpa


com.fasterxml.jackson.dataformat
jackson-dataformat-xml


org.codehaus.woodstox
woodstox-core-asl
4.2.0


mysql
mysql-connector-java






spring-snapshots
http://repo.spring.io/snapshot

true



spring-milestones
http://repo.spring.io/milestone




spring-snapshots
http://repo.spring.io/snapshot


spring-milestones
http://repo.spring.io/milestone






org.springframework.boot
spring-boot-maven-plugin



application.properties

server.port=8081

#server.address=127.0.0.1

#server.sessionTimeout=30
spring.datasource.url=jdbc:mysql://localhost:3306/demo
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.driver-class-name=com.mysql.jdbc.Driver

Example.java

package com.example;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.;
import org.springframework.boot.autoconfigure.
;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.;
import org.springframework.web.bind.annotation.
;

@RestController
@EnableAutoConfiguration
public class Example {
@Autowired
private JdbcTemplate jdbcTemplate;

@RequestMapping("/")
String home() {
    return "Hello World!";
}

@RequestMapping("/thing")
public MyThing thing(){
    return new MyThing();
}

@RequestMapping("/book")
public Book book(){
    return new Book();
}

@RequestMapping("/db")
public void db(){
    List<Map<String, Object>> list=this.jdbcTemplate.queryForList("select * from wxaccount", new Object\[\]{});
    for (int i = 0; i < list.size(); i++) {
         Map<String, Object> map=list.get(i);
         System.out.println(map.get("name"));
    }
}

public static void main(String\[\] args) throws Exception {
    SpringApplication.run(Example.class, args);
}

}

0%