springboot 执行的java代码
@SpringBootApplication
@MapperScan(basePackages = {"cn.xdl.dao"})//扫描Mapper映射器生成对象
//@MapperScan 相当于 (5) 中的 MapperScannerConfigurer
public class MyBootApplication {
public static void main(String[] args) throws SQLException {
ApplicationContext ac = SpringApplication.run(MyBootApplication.class, args);
}
}
application.properties
#datasource
spring.datasource.username=root
spring.datasource.password=123456
spring.datasource.url=jdbc:mysql://localhost:3306/mytest?serverTimezone=UTC
spring.datasource.driverClassName=com.mysql.cj.jdbc.Driver
//可用于@ConfigurationProperties(prefix="spring.datasource") 中的值
#spring.datasource.type=org.apache.commons.dbcp.BasicDataSource//使用非默认数据池
#mybatis //注意 是locations 不是 location
mybatis.mapper-locations=classpath:DeptMapper.xml//不用注解时,mapper的路径
#view //SpringMvc的viewResolver配置
spring.mvc.view.prefix=/ //响应的根地址
spring.mvc.view.suffix=.jsp //响应的后缀名
自定义数据池,如阿里Druid,俩种 赋值 方法
@Configuration
public class DruidDataSourceConfiguration {
@Bean
@ConfigurationProperties(prefix="spring.datasource")
public DataSource druid(){
// DruidDataSource ds = new DruidDataSource();
// ds.setUsername("");
// ds.setPassword("");
// ds.setUrl("");
// ds.setDriverClassName("");
DataSource ds =
DataSourceBuilder.create().type(DruidDataSource.class).build();
return ds;
}
}
Mybatis注解SQL语句,在DAO层直接注解sql语句, 如:
public interface xxxDao{
@Select("select * from student")
List<Student> findAll();
<!-- Mybatis 的各种传参方式 -->
@Update("update student set name=#{name} where id=#{id}")
public void insert(Student stu);//对象的属性,#{属性}
@Update("update student set name=#{0} where id=#{1}")
public void insert(String name,int id);//参数顺序
@Update("update student set name=#{param1} where id=#{param2}")
public void insert(String name,int id);//另外一种参数顺序
@Update("update student set name=#{name} where id=#{id}")
public int updateName(@Param("id")int id,@Param("name")String name);// @Param("a") 相当于 #{a}
}