Spring boot mysql connection

Recent contact with a bit SpringBoot, a "convention is greater than the configured" Micro Framework. Probably use it, and indeed our traditional spring than in the configuration above simplifies a lot, a lot of things do not need to be configured through the web.xml and applicationConetxt.xml. And to start the service, there is a built-in tomcat and jboss container, is also very easy. In general, in fact, it is a collection of libraries that can be used to build systems of any item. I created a maven project, write an example of a case and start a web connection to the database.
1.Web
POM configuration:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
<>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>1.2.5.RELEASE</version>
</dependency>

<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
<version>1.2.5.RELEASE</version>
</dependency> -->

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
<version>1.2.5.RELEASE</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.35</version>
</dependency>

Start springboot then, just springboot-starter-web on it. Then processing method comprising the preparation of a class of HTTP requests and a main () function:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
@Controller
@EnableAutoConfiguration
public class SampleController {

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

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


After starting the main function, can be found in the Control Panel to start a Tomcat container, a Spring MVC-based application also starts up, then visit http: // localhost: 8080 will see the Hello ! World appears in the browser a. Print out the control panel above the spring looked so cool. This configuration is springboot @EnableAutoConfiguration unique role is configured in accordance with application-dependent automatic configuration of the spring, to reduce the amount of development. @Controller @RequestMapping and may be fit to @ResController. Effect is the same. SpringApplication is a helper class, its role is to help start context. So the overall process is the above code SpringApplication run method, and found that this configuration is a Web application (dependent on the class determined in accordance with path), then start the application context in a Spring embedded Tomcat container, and listen to the default tcp port 8080 (default convention). Context while the Spring Spring WebMvc configured in accordance with the default convention:
the Servlet container default Context path /
paths DispatherServlet matched (url-patterns servlet-mapping in) is / *
@ComponentScan path set by default to the same name package SampleController , that is, all @ Controller under this package, after @ Service, @Component, @Repository will be instantiated and added in Spring Context.
Note that, @ ComponentScan default scan all SampleController following bean, so the following example database connection test, SampleController it in the root directory.
2. Database
examples of connections to the database I use mysql, have the opportunity to write an example of a mongoDB. springboot have very good support for relational databases and non-relational database. spring-boot-starter-data- mongodb mongodb is disposed in the maven. Structure of the entire project are:

information DateSource the Configuration document in the resource in a application.properties:

1
2
3
4
spring.datasource.url=jdbc:mysql:
spring.datasource.username=root
spring.datasource.password=wyd
spring.datasource.driver-class-name=com.mysql.jdbc.Driver

bean is the Visitor, say nothing of this, VisitorDao wrote in a method of insert:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
@Component
public class VisitorDao {

static Log log = LogFactory.getLog(VisitorDao.class);

@Autowired
private JdbcTemplate jdbcTemplate;

public boolean insertVistor(final Visitor vistor) {
boolean flag = false;
int i = jdbcTemplate.update("insert into visitor(name,email,status,createtime) values(?,?,?,?)", new Object[] {
vistor.getName(), vistor.getEmail(), vistor.getStatus(), vistor.getCreateTime() });
if (i > 0) {
flag = true;
}
return flag;
}

}

Followed by a service layer, I call run executed inset method to insert data, ok.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
@Component
public class SampleDataService implements CommandLineRunner {

static Log log = LogFactory.getLog(SampleDataService.class);

@Autowired
private VisitorDao visitorDao;

public void run(String... args) {
Visitor visitor = new Visitor();
visitor.setEmail("[email protected]");
visitor.setName("china");
visitor.setStatus(1);
visitorDao.insertVistor(visitor);
log.info("insert in test");
}
}

然后启动SampleController,就会在控制面板看到“inset in test”的输出。插入数据库成功。

原文链接 大专栏  https://www.dazhuanlan.com/2019/08/16/5d55fc4baed17/

Guess you like

Origin www.cnblogs.com/chinatrump/p/11416277.html