Record on pit
MySQL step on pit record
Unknown column ‘zhang3’ in ‘field list’
-- Wrong way
insert into `user`(`user_id`.`user_name`.`user_pwd`) values(3.` fifty `.`wangwu`);
-- The right way
insert into `user`(`user_id`.`user_name`.`user_pwd`) values(3.'Cathy'.'wangwu');
Copy the code
Reason: Use user(the backdouble apostrophe reference above table) for database, table, and field names and ‘triple’ references (next to Enter) for strings.
MyBatis pit record
1. Configure the difference between the mysql url in the.java file and the.xml file
-
In a. Java file
jdbc:mysql://localhost:3306/mybatis? useSSL=true&useUnicode=true&characterEncoding=UTF-8 Copy the code
-
In the.xml file
jdbc:mysql://localhost:3306/mybatis? useSSL=true& useUnicode=true& characterEncoding=UTF-8 Copy the code
Cause: In the. XML file,& cannot be parsed directly, so it needs to be escaped. The corresponding escape character of & is &
2, the paper is not known to the MapperRegistry. abnormal
When writing SQL statements in the. XML file, the. XML file must be registered in the mybatis-config. XML file. Otherwise, Type interface com.jgsu.dao.usermapper is not known to the MapperRegistry. abnormal
<mappers>
<mapper resource="com/jgsu/dao/UserMapper.xml"/>
</mappers>
Copy the code
3, to Could not find the resource com/jgsu/dao/UserMapper. XML anomalies
org.apache.ibatis.builder.BuilderException: Error parsing SQL Mapper Configuration. Cause: java.io.IOException: Could not find resource com/jgsu/dao/UserMapper.xml
Cause :I DEA does not compile SRC Java directory XML file, so in Mybatis configuration file can not find XML file! (There may also be problems with Maven build projects. Many of these projects are normal Java Web projects, so they can be read under SRC.)
Solution: Add nodes to current POM.xml (can be placed at parent level)
<build>
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.xml</include>
</includes>
</resource>
</resources>
</build>
Copy the code
Parsing error was found in mapping #{}
org.apache.ibatis.exceptions.PersistenceException: ### Error building SqlSession. ### The error may exist in com/zuoyueer/dao/UserMapper.xml ### Cause: org.apache.ibatis.builder.BuilderException: Error parsing SQL Mapper Configuration. Cause: org.apache.ibatis.builder.BuilderException: Error parsing Mapper XML. Cause: org.apache.ibatis.builder.BuilderException: Parsing error was found in mapping #{}. Check syntax #{property|(expression), var1=value1, var2=value2, ... } at org.apache.ibatis.exceptions.ExceptionFactory.wrapException(ExceptionFactory.java:30)
at org.apache.ibatis.session.SqlSessionFactoryBuilder.build(SqlSessionFactoryBuilder.java:80)... Caused by: org.apache.ibatis.builder.BuilderException: Error parsing SQL Mapper Configuration. Cause: org.apache.ibatis.builder.BuilderException: Error parsing Mapper XML. Cause: org.apache.ibatis.builder.BuilderException: Parsing error was found in mapping #{}. Check syntax #{property|(expression), var1=value1, var2=value2, ... }Copy the code
There was no value in #{} in my mapping file, so I searched and searched, and added values to all my configurations. Finally, I deleted them one by one, and finally located the exception.
The exception code is as follows:
<select id="getUserById" resultType="com.jgsu.pojo.User" parameterType="int">*/ select 'user_id', 'user_name', 'user_pwd' from 'user' where 'user_id' =#{user_id};</select>
Copy the code
Reason: # {} is contained in annotations, use the comments here is / * * /, will lead to abnormal XMLL file parsing, the annotation of parsing # {}.
Workaround: Delete the comment or annotate it so that the comment content is not parsed.
Parsing error was found in mapping #{}. Mybatis Parsing error was found in Parsing #{}
5, When using Mybatis to perform insert/delete/update operation, it is clear that the operation is successful, but the database data is not updated
SqlSession.com MIT (); sqlSession.com (); Data can be updated in the database.
6. Universal map
Map can be used when there are too many fields or parameters in entity classes or database tables, and we only need to pass partial values in SQL.
Reference: the solution to passing multiple parameters in Mybatis
7, fuzzy query Like
Instead of doing fuzzy queries directly in SQL statements, do %% in Java, as shown below
<! Select * from 'name'; select * from 'name';
<select id="getUserListByLike" resultType="com.jgsu.pojo.User" parameterType="string">
select `user_id`,`user_name`,`user_pwd` from user where `user_name` like #{value};
</select>
<! SQL > select * from 'SQL';
<select id="getUserListByLike" resultType="com.jgsu.pojo.User" parameterType="string">
select `user_id`,`user_name`,`user_pwd` from user where `user_name` like "%"#{value}"%";
</select>
Copy the code
In Java
The interface UserMapper is defined as follows
/** ** ** /
List<User> getUserListByLike(String value);
@Test
public void getUserListByLike(a){
SqlSession sqlSession = MybatisUtils.getSqlSession();
UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
// % indicates any number of characters. Can match characters of any type and length -- concatenated in code
String temp = "%";
List<User> userList = userMapper.getUserListByLike(temp+"Zhang"+temp);
for (User user : userList) {
System.out.println(user);
}
sqlSession.close();
}
Copy the code
8, insert a SQL statement in xxx. XML file into a SQL statement in another XXx2.xml file.
Objective: To achieve multiple students associated with a teacher to achieve many to one query.
Ideas:
1. Compile SQL in TeacherMapper. XML file to query teacher_id. The id in the corresponding Select tag is getTeacherById 2. Studentmapper. XML; However, since the Student class is stored in the teacher class instead of teacherId, resultMap is used to achieve one-to-one correspondence of the attributes listed in the corresponding classes in the database. For complex attributes such as the object teacher, association is used to process and studen is processed The teacher attribute in class T is bound to the t_id in the database. The value of t_id is then bound to the getTeacherById method in TeacherMapper.xml using the Select tag. (which means from TeacherMapper. In XML getTeacherById access to the teacher object then the object to StudentMapper. XML attributes for the teacher to realize the binding to achieve one-to-many query)Copy the code
1 The teacher and student classes in the corresponding POJO package are:
// ----------------------pojo package student class
@AllArgsConstructor // The Lombok plugin simplifies generating set,get,Constructor, and other methods
@NoArgsConstructor
@Data
public class Student {
// id
private int studentId;
// name
private String studentName;
// The corresponding teacher
private Teacher teacher;
}
// ---------------------- teacher class in pojo package
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Teacher {
// id
private int teacherId;
// name
private String teacherName;
}
Copy the code
Alter table student; alter table teacher;
Select teacher_id from teacher where t_id = teacher_idcreate table if not exists `student`(
`student_id` int not null primary key ,
`student_name` varchar(30) not null,
`t_id` int not null
)engine =innodb default charset =utf8; use mybatis; Insert data into student tableinsert into `student`(`student_id`,`student_name`,`t_id`) values (1.'Ming'.1), (2.'little red'.1), (3.'the little green'.1); Create teacher tablecreate table if not exists `teacher`(
`teacher_id` int not null primary key ,
`teacher_name` varchar(30) not null
)engine =innodb default charset =utf8; Insert data into teacher tableinsert into `teacher`(`teacher_id`,`teacher_name`)values (1.'Miss Zhang');
# sqlStatement to implement two tables associated queryselect `student_id`,`student_name`,`teacher_name` from `student`,`teacher` where `t_id` = `teacher_id`;
Copy the code
3 * * * * create TeacherMapper interface, TeacherMapper. XML, StudentMapper interface, StudentMapper. XML is as follows:
- TeacherMapper and StudentMapper interfaces
public interface StudentMapper {
/** ** ** ** /
List<Student> getStudentList(a);
}
public interface TeacherMapper {
/** * Get teacher information from Id ** /
Teacher getTeacherById(@Param("t_id") int id);
}
Copy the code
- TeacherMapper. XML, StudentMapper. XML
<! -- StudentMapper corresponding StudentMapper. An XML file -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- - >
<! DOCTYPEmapper
PUBLIC "- / / mybatis.org//DTD Mapper / 3.0 / EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<! -- Namespace = bind a corresponding DAO /mapper interface path -->
<mapper namespace="com.jgsu.dao.StudentMapper">
<! -- many-to-one query: query multiple students for the same teacher -->
<select id="getStudentList" resultMap="StudentTeacher">
select `student_id`,`student_name`,`t_id` from `student`;
</select>
<resultMap id="StudentTeacher" type="Student">
<result property="studentId" column="student_id"/>
<result property="studentName" column="student_name"/>
<! For complex attributes, separate processing is required: Association handles objects, collection: handles collections
<association property="teacher" column="t_id" javaType="com.jgsu.pojo.Teacher" select="getTeacherById"/><! - note: this should write: com. Jgsu. Dao. TeacherMapper. GetTeacherById - >
</resultMap>
</mapper>
<! -- TeacherMapper corresponding TeacherMapper. An XML file -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- >
<! DOCTYPEmapper
PUBLIC "- / / mybatis.org//DTD Mapper / 3.0 / EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<! -- Namespace = bind a corresponding DAO /mapper interface path -->
<mapper namespace="com.jgsu.dao.TeacherMapper">
<! -- many-to-one query: query multiple students for the same teacher -->
<select id="getTeacherById" resultType="Teacher">
select `teacher_id`,`teacher_name` from `teacher` where `teacher_id` = #{t_id};
</select>
</mapper>
Copy the code
4 Test example:
public class StudentMapperTest {
@Test
public void getStudentList(a){
SqlSession sqlSession = MybatisUtils.getSqlSession();
StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);
List<Student> studentList = mapper.getStudentList();
for(Student student : studentList) { System.out.println(student); } sqlSession.close(); }}Copy the code
Teachermapper.xml: TeacherMapper.xml: teacherMapper.xml: teacherMapper.xml: TeacherMapper.xml: TeacherMapper.xml: TeacherMapper.xml: TeacherMapper.xml: TeacherMapper.xml
org.apache.ibatis.exceptions.PersistenceException:
### Error querying database. Cause: java.lang.IllegalArgumentException: Mapped Statements collection does not contain value for com.jgsu.dao.StudentMapper.getTeacherById
### The error may exist in com/jgsu/dao/StudentMapper.xml
### The error may involve com.jgsu.dao.StudentMapper.getStudentList
### The error occurred while handling results
### SQL: select `student_id`,`student_name`,`t_id` from `student`;
### Cause: java.lang.IllegalArgumentException: Mapped Statements collection does not contain value for com.jgsu.dao.StudentMapper.getTeacherById
Copy the code
The studentmapper. XML file contains a reference to the fully qualified name of the method, not the method name, otherwise it will not be recognized.
<! For complex attributes, separate processing is required: Association handles objects, collection: handles collections
<association property="teacher" column="t_id" javaType="com.jgsu.pojo.Teacher" select="com.jgsu.dao.TeacherMapper.getTeacherById"/>
<! -- Error use case -->
<association property="teacher" column="t_id" javaType="com.jgsu.pojo.Teacher" select="getTeacherById"/>
Copy the code
Spring pit records
1. What is IOC?
My understanding:
Take something that the programmers would have done themselves and hand it over to the client. For example, the programmer provides the customer with three interfaces to connect to SQLServer,mysql and Oracle respectively. Assuming that the user needs to connect to the database mysql, the programmer directly specifies the database as mysql in the business layer, and then the user directly uses it. But after a period of time, the user company has changed, and now the database that needs to be connected is Oracle, so at this time we need to ask the programmer to change the database type in the business layer, what if the user company needs to change the database frequently?? If left to programmers to do it, the programmer is not exhausted, which requires frequent changes to the original code. At this point, we can consider that under the premise of not changing the source code, the operation of switching the database is solved by the user. The programmer only needs to provide the corresponding database interface, and the user can switch the database by calling the corresponding database interface.
Static proxy,JDK dynamic proxy,cglib dynamic proxy main differences
-
The static proxy is implemented at compile time, when the proxy class is an actual class file
-
Dynamic proxies are dynamically generated at run time, meaning that there are no actual class files after compilation, but instead, class bytecodes are dynamically generated at run time and loaded into the JVM. For example, if an interface cannot be instantiated, is it possible to create an instance of an interface at runtime without writing an implementation class? The answer is yes, we can use dynamic proxies to provide instances of an interface that can be created at run time.
-
Objects that use JDK dynamic proxies must implement one or more interfaces
-
Objects using cglib proxies do not need to implement interfaces, so that the proxy class is invasive-free
-
JDK dynamic proxy objects do not need to implement an interface, but they do require that the target object implement an interface, otherwise dynamic proxies cannot be used.
Reference: 1) www.liaoxuefeng.com/wiki/125259…
- Segmentfault.com/a/119000001…