<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/maven-v4_0_0.xsd"> 4.0.0 com.cn springboot-jdbc war 0.0.1-SNAPSHOT springboot-jdbc Maven Webapp http://maven.apache.org org.springframework.boot spring-boot-starter-parent 2.0.3.RELEASE junit junit 3.8.1 test org.springframework.boot spring-boot-starter-web org.springframework.boot spring-boot-starter-test test com.microsoft.sqlserver sqljdbc4 4.0 org.springframework.boot spring-boot-starter-jdbc org.mybatis.spring.boot mybatis-spring-boot-starter 1.3.5 org.mybatis mybatis 3.3.1 --> springboot-jdbc
application.properties配置 我这里用的是SQLserver
1.spring.datasource.url=jdbc:sqlserver://ip:port;DatabaseName=数据库名 spring.datasource.driverClassName=com.microsoft.sqlserver.jdbc.SQLServerDriver spring.datasource.username=sa spring.datasource.password=123
这里添加了一个@MapperScan注解,但是我看别人的项目不加这个注解也可以,只用ComponentScan扫com.vhukze这个二级的包就可以了 ,我的不加不行,不加扫不到mapper包。对于我这个初学者来说 ,还不知道具体原因
@SpringBootApplication @ComponentScan(basePackages = "com.vhukze") @MapperScan("com.vhukze.mapper") public class App { public static void main(String[] args) { SpringApplication.run(App.class, args); } }
@RestController public class JdbcDemoController { @Autowired private JdbcService service; @RequestMapping("/mybatis") public void Jdbc() { service.insert(); } }
@Service public class JdbcServiceImpl implements JdbcService { @Autowired private UserMapper mapper; public void insert() { mapper.insert(); } }
@Mapper public interface UserMapper { @Insert("insert into userDemo values('3','2')") void insert(); }
Copyright © 2023 leiyu.cn. All Rights Reserved. 磊宇云计算 版权所有 许可证编号:B1-20233142/B2-20230630 山东磊宇云计算有限公司 鲁ICP备2020045424号
磊宇云计算致力于以最 “绿色节能” 的方式,让每一位上云的客户成为全球绿色节能和降低碳排放的贡献者