"Engineering Catalog"
"Database table content User table"
"Sqlmapconfig.xml configuration file main content"
Description: The Sqlmapconfig.xml configuration file has two main functions:
1. Information about configuration and data connections, such as transaction management, database connection pooling, and so on.
2. Load the mapping file, such as User.xml in this project
1 <?XML version= "1.0" encoding= "UTF-8"?>2 <!DOCTYPE configuration 3 Public "-//mybatis.org//dtd Config 3.0//en"4 "Http://mybatis.org/dtd/mybatis-3-config.dtd">5 <Configuration>6 7 <!--after integration with spring, these configurations will be removed -8 <Environmentsdefault= "Development">9 <EnvironmentID= "Development">Ten <!--using JDBC Transaction management - One <TransactionManagertype= "JDBC" /> A <!--Database Connection Pool - - <DataSourcetype= "Pooled"> - < Propertyname= "Driver"value= "Com.mysql.jdbc.Driver"/> the < Propertyname= "url" - value= "Jdbc:mysql://localhost:3306/mybatisdb? useunicode=true&characterencoding=utf-8" /> - < Propertyname= "username"value= "root"/> - < Propertyname= "Password"value=""/> + </DataSource> - </Environment> + </Environments> A at <!--Load Map File - - <mappers> - <MapperResource= "Sqlmap/user.xml"/> - </mappers> - - </Configuration>
"User.xml"
Write the associated crud code in the User.xml file
<?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 "><Mappernamespace= "Test"> <SelectID= "Finduserbyid"ParameterType= "int"Resulttype= "com. Higgin.Mybatis.po.User ">SELECT * from USER WHERE Id=#{id}</Select></Mapper>
Explain:
1.namespace namespaces, the role is to classify the SQL management. Note: namespace has a special role to play with Mapper proxy method development.
2. Many SQL statements can be configured in the <mapper> mapping file, except <select> <delete>, <update>, <insert>, etc.
3. Perform a database query through select, encapsulate the SQL statement into the Mapperstatement object, so ID becomes the ID of the statement
4.parameterType: Specifies the type of input parameter, specified here is the int type
5.#{}: Represents a placeholder
6.#{id}: Where the placeholder represents the access input parameters, parameter name is called ID, if the input parameter is a simple type, #{} parameter name can be arbitrary, can be value or other name
7.resultType: Specifies the Java type object to which the SQL output results are mapped, andselect specifies that the Resulttype represents a user object that maps a single record
"Log4j.properties"
#Global Logging configuration# in the development environment, the log level is set to DEBUGlog4j.rootlogger=debug,stdout#Console Output ... log4j.appender.stdout=org.apache.log4j.ConsoleAppenderlog4j.appender.stdout.layout= Org.apache.log4j.PatternLayoutlog4j.appender.stdout.layout.conversionpattern=%5p [%t]-%m%n
"User.java"
1 Packagecom. Higgin.Mybatis.po;2 3 Importjava.util.Date;4 5 Public classUser {6 //property name corresponds to database table field7 Private intID;8 PrivateString username;9 PrivateString sex;Ten PrivateDate birthday; One PrivateString address; A //ignore get, set, tostring method .... -}
"Mybatistest.java"
Public classmybatistest {@Test Public voidTestfinduserbyid ()throwsioexception{//mybatis configuration fileString resource= "Sqlmapconfig.xml"; //Get configuration fileInputStream inputstream=Resources.getresourceasstream (Resource); //creating a session factory, passing in MyBatis profile informationSqlsessionfactory sqlsessionfactory=NewSqlsessionfactorybuilder (). Build (InputStream); //get sqlsession through the factorySqlsession sqlsession=sqlsessionfactory.opensession (); //manipulating databases with sqlsession//First parameter: The ID of the statement in the mapping file, equal to: namespace+ "." ID of the +statement//second parameter: Specify the parameters of the matching parametertype type in the mapping fileUser User=sqlsession.selectone ("Test.finduserbyid", 2); System.out.println (User.tostring ()); //Freeing ResourcesSqlsession.close (); }}
"Run Results"
01_ querying user data by ID