文章目录
一.Spring boot 常用注解
2@EnableAutoConfiguration:自动配置
1.$站位符的使用
1.@SpringBootApplication:启动
SpringBoot的核心注解,主要目的是开启自动配置。它也是一个组合注解,主要组合了@Configuration,@EnableAutoConfiguration(核心)和@ComponentScan。可以通过@SpringBootApplication(exclude={想要关闭的自动配置的类名.class})来关闭特定的自动配置,其中@ComponentScan让spring Boot扫描到Configuration类并把它加入到程序上下文。
2@EnableAutoConfiguration:自动配置
此注释自动载入应用程序所需的所有Bean——这依赖于Spring Boot在类路径中的查找。该注解组合了@Import注解,@Import注解导入了EnableAutoCofigurationImportSelector类,它使用SpringFactoriesLoader.loaderFactoryNames方法来扫描具有META-INF/spring.factories文件的jar包。
3.@Configuration:配置文件
等同于
spring
的XML
配置文件;使用Java
代码可以检查类型安全
4.@ComponentScan:自动扫描
表示将该类自动发现扫描组件。个人理解相当于,如果扫描到有
@Component、@Controller、@Service
等这些注解的类,并注册为Bean
,可以自动收集所有的Spring
组件,包括@Configuration
类。
数据框架一mybatis
简介:
MyBatis是一个开源的数据持久层框架。内部封装了通过JDBC访问数据库的操作,支持普通的SQL查询、存储过程和高级映射,几乎消除了所有的JDBC代码和参数的手工设置以及结果集的检索。
MyBatis是一款优秀的持久层框架,它支持定制化SQL、存储过程以及高级映射。MyBatis避免了几乎所有的JDBC代码和手动设置参数以及获取结果集。MyBatis可以使用简单的XML或注解来配置和映射原生信息,将接口和Java的POJO(Plain Old Java Objects,普通Java对象)映射成数据库中的记录
MyBatis作为持久层框架,主要思想是将程序中的大量SQL语句剥离出来,配置在配置文件中,实现SQL的灵活配置。这样做的好处是将SQL与程序代码分离,可以在不修改程序代码的情况下,直接在配置文件中修改SQL
$站位符的使用
特点:
1.MyBatis处理 #{ } 占位符,使用的 JDBC 对象是 PreparedStatement 对象,执行sql语句的效率更高。
2.使用 PreparedStatement 对象,能够避免 sql 注入,使得sql语句的执行更加安全。
3.#{ } 常常作为列值使用,位于sql语句中等号的右侧;#{ } 位置的值与数据类型是相关的。
使用$占位符对数据的操作
package com.bjpowernode.dao;
import com.bjpowernode.entity.Student;
import org.apache.ibatis.annotations.Param;
import java.util.List;
/**
*
*/
public interface StudentDao
{
List<Student> queryStudent(@Param("studentName") String name);
}
<!-- ${}占位符的使用 -->
<select id="queryStudent" resultType="com.bjpowernode.entity.Student">
select * from student where name=${studentName}
</select>
@Test
public void testUpdateStudent() {
SqlSession session=MyBatisUtil.getSqlSession();
StudentDao studentDao=session.getMapper(StudentDao.class);
Student student=new Student();
student.setId(1003);
student.setName("方法还是快点");
student.setEmail("8888888@qq.com");
student.setAge(20);
int rows=studentDao.updateStudent(student);
session.commit();
System.out.println("更新学生的rows === " + rows);
session.close();
}
占位符使用
package com.bjpowernode.dao;
import com.bjpowernode.entity.Student;
import org.apache.ibatis.annotations.Param;
import java.util.List;
/**
*
*/
public interface StudentDao {
//${}占位符的使用,使用@Param命名参数
List<Student> queryStudent(@Param("studentName") String name);
}
<!-- ${}占位符的使用 -->
<select id="queryStudent" resultType="com.bjpowernode.entity.Student">
select * from student where name=${studentName}
</select>
@Test
public void testQueryStudent() {
SqlSession session= MyBatisUtil.getSqlSession();
StudentDao studentDao=session.getMapper(StudentDao.class);
List<Student> students=studentDao.queryStudent("古语");
for(Student stu : students) {
System.out.println("stu === " + stu);
}
session.close();
}
这里注意代码中通过 ${ } 占位符传值的地方,如果我们写成下面这样,代码运行一定是会报错的!!!
List<Student> students=studentDao.queryStudent("古语");
这是因为 ${ } 占位符中的数据是原模原样的,不会区分数据类型。也就是说它会把你的mapper文件中的sql语句转换为:select * from student where name=古语 (错误!!!)
对sql语句有了解的人肯定都能看出这里的错误,首先我们的name字段的数据类型是 varchar,而这里 name=张起灵 显然是错误的,应该是 name= '张起灵' 这样才对,所以代码中通过 ${ } 占位符传值的地方,应该这样写:👇👇👇List<Student> students=studentDao.queryStudent("古语");
对参数 张起灵 添加引号,根据 ${ } 占位符中的数据是原模原样的,此时它会把你的mapper文件中的sql语句转换为:select * from student where name='古语' (正确!!!)