First, acquaintance Mybatis
1. adding the relevant jar package.
2. Create the corresponding entity class bean.
3. Create mybatis profile.
4. Create mybatis mapping file.
5. mybatis mapping file to be introduced into the profile.
6. Test.
Third, optimization
1. If the query is a plurality of mapping file, can be used as a parameter set Map
2. The property file listed separately db.recourses, you need to import the file attributes Mybatis profile, after use $ {}
<properties resource="db.properties"></properties>
${jdbc.driver}
3. aliases for an entity class for all classes from the package under an alias, the alias for the class name (not recommended, others not convenient interpretation of the code)
<typeAliases>
<package name="com.zhiyou100.klb.bean"/>
</typeAliases>
4. Add the log information log4j.properties, to help us troubleshoot
The use of the interface in conjunction xml file
1. Create an interface, the interface and mapping files to match. Method name name = ID
2. mapping file, namespace to the file path has been mapped, for example: com.zhiyou100.klb.dao.UsersDao
3. Test program operating conditions,
// Analytical conf.xml
Reader = Resources.getResourceAsReader Reader ( "conf.xml");
// Get the object sessionFactory
a SqlSessionFactory sessionFactory the SqlSessionFactoryBuilder new new = () Build (Reader);.
// Get session object for operating a database
session = sessionFactory.openSession ();
// get interface implementation class
usersDao = session.getMapper (UsersDao.class);
Fifth, to solve the fields in the database with the class attribute mismatch
1. In the SQL statement to field the same starting alias attribute names, aliases and class
2. Using resultMap
<! - references resultMap tag ->
<the SELECT the above mentioned id = "getClazz" parameterType = "int" resultMap = "clazzMap"> </ the SELECT>
<- resultMap:! Written correspondence between mathematics and field
type: which entity representation and the correspondence relation table class
->
<the resultMap type = "com.zhiyou100.klb.bean.Clazz" ID = "clazzMap">
<- ID:! indicates a correspondence relationship attribute table with the primary key of entity class - ->
<ID = column "c_id" Property = "CID" />
<Result column = "c_name" Property = "CNAME" />
<Result column = "teacher_id" Property = "TID" />
</ The resultMap>
Sixth, contingency table query
<! - resultMap reference tag ->
<SELECT ID = "getClazz" the parameterType = "int" = resultMap "clazzMap">
SELECT * from class C, T Teacher, Student WHERE c.teacher_id S and C = t.t_id. = s.class_id and c.c_id = c_id # {ID}
</ SELECT>
<- the resultMap:! write field corresponding relationship between the mathematics and
type: a correspondence relationship table which indicates the entity class
->
<the resultMap type = " com.zhiyou100.klb.bean.Clazz "ID =" clazzMap ">
<- ID:! indicates a correspondence relationship attribute table with the primary key of entity class ->
<ID = column" c_id "property =" CID " />
<Result column = "c_name" Property = "CNAME" />
<Result column = "teacher_id" Property = "TID" />
<Association Property = "Teacher"javaType="com.zhiyou100.klb.bean.Teacher">
<id column="t_id" property="tid"/>
<result column="t_name" property="tname"/>
</association>
<collection property="students" ofType="com.zhiyou100.klb.bean.Student">
<id column="s_id" property="sid"/>
<result column="s_name" property="sname"/>
<result column="class_id" property="cid"/>
</collection>
<!-- <association property="teacher" javaType="com.zhiyou100.klb.bean.Teacher"
column="teacher_id" select="com.zhiyou100.klb.dao.TeacherDao.getTeacher">
</association> -->
</resultMap>
Distinguish seven, Mybatis in $ {} and {#} of
- $: Does not add to the content parsing "" He is the existence of harm splicing sql injection sql statement. When passed to table structure.
- #: Added "" placeholder when using its sql, sql injection to prevent content parsing.
Eight, how to return the object ID when you add
<insert id="add" parameterType="Users" useGeneratedKeys="true" keyProperty="id">
insert into users(name,age) values(#{name},#{age})
</insert>