spring boot集成mysql(二)

在第一个的基础之上开始修改pom.xml

<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>ccy</groupId>
  <artifactId>springBootMysql</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>jar</packaging>


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


<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<!-- 引入spring-boot -->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.4.3.RELEASE</version>
</parent>
<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>

<!-- 引入jpa和mysql -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency> 
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>

这里使用的是jpa的方式

resource.propery文件内容

#DB Configuration:
spring.datasource.driverClassName = com.mysql.jdbc.Driver
spring.datasource.url = jdbc:mysql://localhost:3306/second
spring.datasource.username = root
spring.datasource.password = root


#JPA Configuration:  
spring.jpa.database=MySQL
spring.jpa.show-sql=true  
spring.jpa.generate-ddl=true  
spring.jpa.hibernate.ddl-auto=update  
#spring.jpa.database-platform=org.hibernate.dialect.MySQL5Dialect  
spring.jpa.hibernate.naming_strategy=org.hibernate.cfg.ImprovedNamingStrategy  
#spring.jpa.database=org.hibernate.dialect.MySQL5InnoDBDialect 
#spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MYSQL5Dialect

-------------------------------下面我们看一下结构和具体的解释----------------------------------------------------


1.配置文件的名字和路径都是在框架固定好的,所以只要出现这个文件就会自动扫描而不用mvc一样显式配置

2.start文件

package ccy.springBootMysql;


import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;


@SpringBootApplication
@RestController
public class StartMainBase {
    public static void main(String[] args) throws Exception {
    System.out.println("222");
        SpringApplication.run(StartMainBase.class, args);
        
    }
}

=============这里需要注意几个事情=========

@RestController=@Controller+@Response 这个是必须有的 不然将会导致IOC注入异常,无法扫描到相应的注解

@SpringBootApplication 整合了 @Configuration @EnableAutoConfiguration   @ComponentScan 当没有特殊声明的时候将会扫描自己包下的所有注解而不是整个项目的


package ccy.springBootMysql.start;


import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;


//@SpringBootApplication(scanBasePackages={"ccy.springBootMysql.controller","ccy.springBootMysql.dao","ccy.springBootMysql.demo"})
//@RestController
public class StartMain {
    public static void main(String[] args) throws Exception {
    System.out.println("222");
        SpringApplication.run(StartMain.class, args);
        
    }
}

这种方式就可以扫描到其他位置了,但是注意两个类不要同时启动啊

----------------------------------说完启动看挂载数据库 也就是dao层-----------------------------------------------------------

ackage ccy.springBootMysql.dao;


import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;
import ccy.springBootMysql.demo.User;
public interface UserRepository extends JpaRepository<User,Long>{
}

-----------------------是的你没有看错就是一个空载的interface,而且没有daoImpl ,而且看好了没有注解哦!!!! 那为什么一个空载的Service就能搞事情了--------------------------------

我们查看源码就会发现一个事情

@Repository
@Transactional(readOnly = true)
public class SimpleJpaRepository<T, ID extends Serializable>
implements JpaRepository<T, ID>, JpaSpecificationExecutor<T> {

......中间若干反射实现

}

----------------------原来是利用了spring jpa自己的实现  所以整出来一个特别不适应的空载.....以前都得自己整个baseDao 或者数据库映射啥的-------------------

下面是实体类 /数据库 建个表,字段名相同就行 我就不复述了

package ccy.springBootMysql.demo;


import java.math.BigDecimal;


import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;


@Entity
public class User {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
@Column
private String username;
@Column
private Integer age;
@Column
private BigDecimal balance;


public Long getId() {
return id;
}


public void setId(Long id) {
this.id = id;
}


public String getUsername() {
return username;
}


public void setUsername(String username) {
this.username = username;
}


public Integer getAge() {
return age;
}


public void setAge(Integer age) {
this.age = age;
}


public BigDecimal getBalance() {
return balance;
}


public void setBalance(BigDecimal balance) {
this.balance = balance;
}


}

------------------------------------然后好吧 我承认我懒了 我直接把dao 挂载到了 controller 当然你要愿意也可以 用service 我的源码里面有-----------------------------------------------

package ccy.springBootMysql.controller;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


import ccy.springBootMysql.dao.UserRepository;
import ccy.springBootMysql.demo.User;


@RestController
@RequestMapping("userController")
public class UserController {
@Autowired
private UserRepository userRepository;


/***
* @GetMapping 等效于 @RequstMapp(value="GET")

*/


@RequestMapping(value = "findById")
public User findById(Long id) {
System.out.println("---接到id--" + id);
User user = this.userRepository.findOne(id);
/*User user = new User();
user.setId(213L);*/
return user;
}
}

-----------------调用完以后美美哒---------------------




猜你喜欢

转载自blog.csdn.net/habazhu1110/article/details/77985282