At present, I am learning mybatis. The best helper to learn mybatis is his official website https://mybatis.org/mybatis-3/zh , and check the official source code of mybatis. You can learn in detail how mybatis is used to eliminate the jdbc connection code, and how jdbc sets to obtain the values of specific fields in the database. The returned specific result sets are encapsulated. We only need to set them in the global mapping file. Of course, the header of the global mapping file is best copied from the mybatis official website, Otherwise, you may have input errors, and then you need to configure the corresponding tags.
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd">
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd"> <configuration> <properties resource="jdbc.props"></properties> <settings> <setting name="logImpl" value="LOG4J"/> <!-- <setting name="lazyLoadingEnabled" value="true"/> --> </settings> <typeAliases> <package name="org.lanqiao.mybatis01.entity"/> </typeAliases> <environments default="dev"> <environment id="dev"> <transactionManager type="JDBC"></transactionManager> <dataSource type="POOLED"> <property name="driver" value="$"/> <property name="url" value="$"/> <property name="username" value="$"/> <property name="password" value="$"/> </dataSource> </environment> <environment id="test"> <transactionManager type="JDBC"></transactionManager> <dataSource type="POOLED"> <property name="driver" value="com.mysql.jdbc.Driver"/> <property name="url" value="jdbc:mysql://112.18.0.1:3306/test?useUnicode=true&characterEncoding=UTF-8"/> <property name="username" value="root"/> <property name="password" value="123"/> </dataSource> </environment> <environment id="pro"> <transactionManager type="JDBC"></transactionManager> <dataSource type="POOLED"> <property name="driver" value="com.mysql.jdbc.Driver"/> <property name="url" value="jdbc:mysql://112.181.10.51:3306/mybatis?useUnicode=true&characterEncoding=UTF-8"/> <property name="username" value="root"/> <property name="password" value="123"/> </dataSource> </environment> </environments> <mappers> <mapper resource="org/lanqiao/mapper/StudentMapper.xml"/> <mapper resource="org/lanqiao/mapper/SgroupMapper.xml"/> <mapper resource="org/lanqiao/mapper/CouresMapper.xml"/> </mappers> </configuration>
Basically, a dao layer interface writes a mapper.xml mapping file, which now reflects the usefulness of the interface. In the previous telecom tariff practical project, it has always been thought that the interface is of little use. The sql statements originally in the implementation class of the interface are written in its corresponding mapping file. The following is the specific content written in StudentMapper.xml in the above figure, It is mainly about the writing of sql statements, so we must write sql statements skillfully in development.
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="org.lanqiao.mybatis01.dao.StudentDao"> <resultMap type="Student" id="studentResultMap"> <id property="sid" column="sid"/> <result property="sname" column="sname"/> <result property="sage" column="sage"/> <association property="sgroup" column="gid" select="org.lanqiao.mybatis01.dao.SgroupDao.selectSgroupByGid"></association> <collection property="lc" column="sid" select="org.lanqiao.mybatis01.dao.CouresDao.selectCouresBySid"></collection> </resultMap> <sql id="selectAllStudent">sid,sname,sage,gid</sql> <insert id="insertStudent"> insert into student values(null,#,#) </insert> <delete id="deleteStudent"> delete from student where sid = # </delete> <select id="selectAllStudent" resultMap="studentResultMap"> select * from student </select> <select id="selectStudentByCount" resultType="Student" > select <include refid="selectAllStudent"/> from student <where> <if test="student.sid!=null"> and sid=# </if> <if test="student.sname!=''and student.sname!=null"> and sname like concat('%',concat(#,'%')) </if> <if test="student.sage!=null"> and sage=# </if> </where> </select> <select id="selectStudentBySname" parameterType="string" resultType="Student"> select * from student group by $ desc </select> <select id="selectStudentBySids" resultType="Student"> select * from student where sid in <foreach collection="list" item="student" open="(" close=")" separator="," > # </foreach> </select> <insert id="insertStudents"> insert into student(sid,sname,sage) values <foreach collection="list" item="student" separator=","> (null,#,#) </foreach> </insert> <delete id="deleteStudents"> delete from student where sid in <foreach collection="list" item="student" open="(" close=")" separator=","> # </foreach> </delete> <select id="selectStudentsBySid" resultMap="studentResultMap"> select <include refid="selectAllStudent"></include> from student where sid = # </select> <update id="updateStudentBySid"> update student set sname = # where sid = # </update> </mapper>
My understanding of ${}, #{} is that the former takes the contents in parentheses and will be directly spliced into sql statements. Using it may cause sql injection problems, and the latter takes @ Param(“ ") It is recommended to directly define a name for the required parameters in the interface. This problem is described in detail in some articles of csdn. I have benefited a lot. I must fill csdn with a member after work. Hahaha, then I learned the dynamic splicing of sql. Generally, the foreach tag is used in the sql statement only when the incoming parameter type is a collection. In fact, there are few foreach tags, I used < where >, < if test = "" >, < choose > < when >, and then learned the caching mechanism of mybatis, which has primary and secondary caching mechanisms.
In popular terms, caching is to save the records queried from the database into memory, and then directly read the data in the cache the next time you query the same content, rather than query in the database
The first level cache in mybatis is enabled by default. Its scope of use is the same sqlsession. There are three methods to close the cache of mybatis. The cache can simulate the effect by creating two sqlsession objects,
The L2 cache is enabled by setting < Settings > in the global configuration file. However, I checked the official document of mybatis and found that it is also enabled by default. However, to use it, you need to use < cache / > in the specific mapper.xml to act on all select statements of the entire namespace. There are two algorithms used by the L2 cache. One is the LUR algorithm (used at least recently), Put the data found in the cache at the end of the linked list. If the cache is full, put the newly inserted data at the end and delete the data at the front of the linked list. The other is the FIFO algorithm. Later, we also learned the very troublesome problem in the previous project, that is, we need to query again to obtain the value of its self increasing ID after adding information, You only need to set the properties in the < Insert > tag. ID refers to the interface name, keyProperty refers to the primary key name, and useGeneratedKeys refers to the information returned from the primary key
<insert id="insertStudent" keyProperty="sid" useGeneratedKeys="true">
static InputStream is; static SqlSession sqlSession; static SqlSession sqlSession2; static StudentDao studentDao; static StudentDao studentDao2; @BeforeClass public static void init() throws IOException { //Step 1: read the global configuration file mybatis-config.xml into the stream is = Resources.getResourceAsStream("mybatis01-conf.xml"); //Step 2: create a factory builder of sqlsession through the flow (global configuration file) SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is); //Step 3: create the sqlsession object through the sqlsession factory sqlSession = sqlSessionFactory.openSession(); sqlSession2 = sqlSessionFactory.openSession(); //Step 4: obtain the object of StudentDao through sqlsession studentDao = sqlSession.getMapper(StudentDao.class); studentDao2 = sqlSession2.getMapper(StudentDao.class); }
Then I learned how to replace xml files with annotations and directly use annotations in the interface. The first learning is over. After so long, I sent scdn on the first day!!!!