When learning MyBatis, I suddenly encountered a test error. Test Mapper Agent failed, now delve less, do not like to see the unknown error, immediately correct. Error printing says Mapper.xml has been registered, look closely at Sqlmapconfig.xml found indeed. Normal after modification.
The reason is to copy the teaching code directly, in Sqlmapconfig.xml, configure the mappers, load the mapping file, already configured the load package file, and then I added a separate mapper. So something went wrong, comment out the package, OK.
Test Case:
1 @Before2 Public voidSetUp ()throwsException {3 //Create Sqlsessionfactory4 //mybatis configuration file5String resource = "Sqlmapconfig.xml";6 //get configuration file stream7InputStream InputStream =Resources.getresourceasstream (Resource);8 //creating a session factory, passing in MyBatis profile information9Sqlsessionfactory =NewSqlsessionfactorybuilder ()Ten . Build (InputStream); One A } - - @Test the Public voidTestfinduserbyid ()throwsexception{ -Sqlsession sqlsession =sqlsessionfactory.opensession (); -Usermapper usermapper = Sqlsession.getmapper (usermapper.class); -User user = Usermapper.finduserbyid (1); + System.out.println (user); - +}
Sqlmapconfig.xml
1<?xml version= "1.0" encoding= "UTF-8"?>2<!DOCTYPE Configuration3Public "-//mybatis.org//dtd Config 3.0//en"4"Http://mybatis.org/dtd/mybatis-3-config.dtd" >5<configuration>6 7<!--load Properties file--8<properties resource= "Db.properties" >9<!--properties You can also configure some property names and property values-Ten<!--<property name= "jdbc.driver" value= ""/>- One</properties> A<!--global configuration parameters, when needed and set- -<!--<settings> - the</settings> - -<!--alias Definition-- -<typeAliases> + -<!--defined for a single alias + Type : The path of the types A Alias : Aliases at- -<!--<typealias type= "Cn.itcast.mybatis.po.User" alias= "User"/>- -<!--Bulk alias definition - Specifies the package name, MyBatis automatically scans the PO class in the package, and automatically defines the alias, which is the class name (either uppercase or lowercase) -- -< PackageName= "Cn.itcast.mybatis.po"/> in -</typeAliases> to +<!--and spring are integrated after the environments configuration will be abolished-- -<environmentsdefault= "Development" > the<environment id= "Development" > *<!--using JDBC Transaction management, transaction control by mybatis--> $<transactionmanager type= "JDBC"/>Panax Notoginseng<!--database connection pool, managed by MyBatis -<datasource type= "Pooled" > the<property name= "Driver" value= "${jdbc.driver}"/> +<property name= "url" value= "${jdbc.url}"/> A<property name= "username" value= "${jdbc.username}"/> the<property name= "Password" value= "${jdbc.password}"/> +</dataSource> -</environment> $</environments> $<!--load a mapping file-- -<mappers> -<mapper resource= "Sqlmap/user.xml"/> the<mapper resource= "Mapper/usermapper.xml"/> - Wuyi<!--load one mapping file at a time with the resource method-- the<!--<mapper resource= "Mapper/usermapper.xml"/>-- - Wu<!--loading a single mapping file via the Mapper interface - Follow some specifications: You need to keep the Mapper interface class name and the Mapper.xml mapping file name consistent and in one directory About The prerequisite for the above specification is that the Mapper proxy method is used $- -<!--<mapperclass= "Cn.itcast.mybatis.mapper.UserMapper"/>-- - -<!--Bulk Load Mapper A Specifies the package name of the Mapper interface, MyBatis automatically scans all mapper interfaces underneath the package for loading + Follow some specifications: You need to keep the Mapper interface class name and the Mapper.xml mapping file name consistent and in one directory the The prerequisite for the above specification is that the Mapper proxy method is used -- $<!--< PackageName= "Cn.mrf.mybatis.mapper"/>-- the the</mappers> the the</configuration>View Code
Usermapper.xml
1<?xml version= "1.0" encoding= "UTF-8"?>2<!DOCTYPE Mapper3Public "-//mybatis.org//dtd Mapper 3.0//en"4"Http://mybatis.org/dtd/mybatis-3-mapper.dtd" >5 6<!--namespace namespaces, the role is to classify the SQL management, understand the SQL isolation7 Note: namespace has a special role to play with Mapper proxy method development.8-9<mapper namespace= "Cn.mrf.mybatis.mapper.UserMapper" >Ten One<!--Configure many SQL statements in the mapping file-- A<!--requirements: Querying user table records by ID-- -<!--perform a database query with select - ID: Identifies the SQL in the mapping file the encapsulates an SQL statement into an Mappedstatement object, so ID is called statement ID - parametertype: Specifies the type of input parameter, where the int type is specified - #{} represents a placeholder symbol - #{id}: Where the ID represents the input parameter, the parameter name is the ID, if the input parameter is a simple type, the parameter name in #{} can be arbitrary, can be value or another name + - Resulttype: Specifies the type of Java object to which the SQL output results are mapped, and select Specifies that Resulttype represents a Java object that maps a single record. +- A<select id= "Finduserbyid" parametertype= "int" resulttype= "Cn.mrf.mybatis.po.User" > atSELECT * from USER WHERE id=#{value} -</select> - -<!--based on user name fuzzy query user information, may return more than one - Resulttype: Specifies the type of Java object to which a single record is mapped - ${}: Represents the concatenation of the SQL string, and the contents of the received parameter are stitched together in SQL without any modification. in stitching SQL using ${}, causing SQL injection - ${value}: Receives the contents of the input parameter, if the incoming type is a simple type, ${} can only use the value to- +<select id= "Finduserbyname" parametertype= "java.lang.String" resulttype= "Cn.mrf.mybatis.po.User" > -SELECT * from USER WHERE username like '%${value}% ' the</select> * $<!--Add UserPanax Notoginseng ParameterType: Specifies that the input parameter type is Pojo (including user information) - #{} Specifies the property name of the Pojo, receives the property value of the Pojo object, MyBatis gets the object's property value through OGNL the- +<insert id= "Insertuser" parametertype= "Cn.mrf.mybatis.po.User" > A<!-- the returns the primary key of the inserted data back to the user object + - SELECT last_insert_id (): Gets the primary key value of the first INSERT in the record, only applicable with the self-increment primary key $ $ keyproperty: Sets the query to primary key value to parametertype the specified object's property - order:select last_insert_id () execution order, relative to the INSERT statement, of its execution order - Resulttype: Specifies the result type of select LAST_INSERT_ID () the- -<selectkey keyproperty= "id" order= "after" resulttype= "Java.lang.Integer" >Wuyi SELECT last_insert_id () the</selectKey> - INSERT INTO User (username,birthday,sex,address) value (#{username},#{birthday},#{sex},#{address}) Wu<!-- - using MySQL's uuid () to generate a primary key About Execution Process: $ The primary key is first obtained through the UUID () and the primary key is set to the id attribute of the user object - Second, when the insert executes, remove the id attribute value from the user object -- -<!--<selectkey keyproperty= "id" order= "before" resulttype= "java.lang.String" > A SELECT uuid () +</selectKey> theInsert into User (id,username,birthday,sex,address) value (#{id},#{username},#{birthday},#{sex},#{address})- - $ the</insert> the the<!--Delete User the To delete a user by ID, you need to enter an ID value -- in<delete id= "DeleteUser" parametertype= "Java.lang.Integer" > theDelete from user where id=#{id} the</delete> About the<!--update users by ID the Analysis: the the ID of the incoming user is required + update information required for incoming users - ParameterType Specify the user object, including ID and update information, note: ID must exist the #{id}: Gets the id attribute value from the input user objectBayi- the<update id= "UpdateUser" parametertype= "Cn.mrf.mybatis.po.User" > theUpdate user Set username=#{username},birthday=#{birthday},sex=#{sex},address=#{address} -where id=#{id} -</update> the the</mapper>View Code
MyBatis Learning Error: Repeating load Mapper.xml