夜光带你走进我擅长的领域:SpringBOOT(3)

夜光序言:

 

 

到了最后,我们还是连一句不咸不淡的问候都没有了。

不要再折磨我,我的心已被割破,流尽的不是血,是爱你的错。爱情就像乘法,其中一项为零,其结果永远为零。

 

 

 

 

 

正文:

3.5、使用JSP渲染Web视图

3.5.1、pom文件引入以下依赖

<parent>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-starter-parent</artifactId>

<version>1.3.3.RELEASE</version>

</parent>

<dependencies>

<!-- SpringBoot 核心组件 -->

<dependency>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-starter-web</artifactId>

</dependency>

<dependency>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-starter-tomcat</artifactId>

</dependency>

<dependency>

<groupId>org.apache.tomcat.embed</groupId>

<artifactId>tomcat-embed-jasper</artifactId>

</dependency>

</dependencies>

 

3.5.2、在application.properties创建以下配置

spring.mvc.view.prefix=/WEB-INF/jsp/

spring.mvc.view.suffix=.jsp

 

3.5.3、后台代码

@Controller

public class IndexController {

@RequestMapping("/index")

public String index() {

return "index";

}

}

 

 

 

 

 

 

 

 

 

 

 

 

 

 

  • 数据访问

4.1springboot整合

使用jdbctemplate

4.1.1 pom文件引入

<parent>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-starter-parent</artifactId>

<version>1.5.2.RELEASE</version>

</parent>

<dependencies>

<dependency>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-starter-jdbc</artifactId>

</dependency>

<dependency>

<groupId>mysql</groupId>

<artifactId>mysql-connector-java</artifactId>

<version>5.1.21</version>

</dependency>

<dependency>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-starter-test</artifactId>

<scope>test</scope>

</dependency>

<dependency>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-starter-web</artifactId>

</dependency>

</dependencies>

4.1.2 application.properties新增配置

有的公司单位用.yml

也挺不错

spring.datasource.url=jdbc:mysql://localhost:3306/test

spring.datasource.username=root 

spring.datasource.password=root

spring.datasource.driver-class-name=com.mysql.jdbc.Driver

 

 

4.1.3 UserService

@Service

public class UserServiceImpl implements UserService {

@Autowired

private JdbcTemplate jdbcTemplate;

public void createUser(String name, Integer age) {

System.out.println("ssss");

jdbcTemplate.update("insert into users values(null,?,?);", name, age);

}

}

 

4.1.4 App

@ComponentScan(basePackages = "com.Genius")

@EnableAutoConfiguration

public class App {

public static void main(String[] args) {

SpringApplication.run(App.class, args);

}

}

 

嗯唔,注意: spring-boot-starter-parent1.5以上

4.2springboot整合使用mybatis

4.2.1pom文件引入

<parent>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-starter-parent</artifactId>

<version>1.3.2.RELEASE</version>

<relativePath /> <!-- lookup parent from repository -->

</parent>

<dependencies>

<dependency>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-starter</artifactId>

</dependency>

<dependency>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-starter-test</artifactId>

<scope>test</scope>

</dependency>

<dependency>

<groupId>org.mybatis.spring.boot</groupId>

<artifactId>mybatis-spring-boot-starter</artifactId>

<version>1.1.1</version>

</dependency>

<dependency>

<groupId>mysql</groupId>

<artifactId>mysql-connector-java</artifactId>

<version>5.1.21</version>

</dependency>

<dependency>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-starter-web</artifactId>

</dependency>

</dependencies>

 

4.2.2、配置文件引入

spring.datasource.url=jdbc:mysql://localhost:3306/test

spring.datasource.username=root

spring.datasource.password=root

spring.datasource.driver-class-name=com.mysql.jdbc.Driver

 

 

4.2.3Mapper代码

public interface UserMapper {

@Select("SELECT * FROM USERS WHERE NAME = #{name}")

User findByName(@Param("name") String name);

@Insert("INSERT INTO USERS(NAME, AGE) VALUES(#{name}, #{age})")

int insert(@Param("name") String name, @Param("age") Integer age);

}

 

 

4.2.4、启动方式

@ComponentScan(basePackages = "com.Genius")

@MapperScan(basePackages = "com.Genius.mapper")

@SpringBootApplication

public class App {

public static void main(String[] args) {

SpringApplication.run(App.class, args);

}

}

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值