1.创建一个maven web项目
SQLserver的依赖
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>com.microsoft.sqlserver</groupId> <artifactId>sqljdbc4</artifactId> <version>4.0</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jdbc</artifactId> </dependency>
3.在application.properties配置文件中添加配置
MySQL的配置
spring.datasource.url=jdbc:sqlserver://localhost:1433;DatabaseName=xxglptcsDB spring.datasource.driverClassName=com.microsoft.sqlserver.jdbc.SQLServerDriver spring.datasource.username=dd spring.datasource.password=dd
4.写代码
项目结构
controller层
@EnableAutoConfiguration @ComponentScan(basePackages="com.vhukze") @RestController public class JdbcDemoController { @Autowired private JdbcService service; @RequestMapping("/jdbc") public void Jdbc() { service.insert(); } public static void main(String[] args) { SpringApplication.run(JdbcDemoController.class, args); } }
service层
@Service public class JdbcServiceImpl implements JdbcService { @Autowired private JdbcTemplate jdbc; public void insert() { jdbc.update("insert into userDemo values(?,?)","123","123"); } }
5.启动项目访问controller层方法映射路径
结果