Mybatis Basic Series I
Create the first mybatis program
Need to configure items
1. Configuration of two tag database connections and mapper in conf.xml, loading information of XML file
<-- Configuration of database environment parameters default There is a database environment that needs to be adopted, because there may be development,test Various environments --> <environments default="development"> <environment id="development"> <transactionManager type="JDBC"/> <datasource type="POOLED"> <property name="driver", value=""/> <property name="url", value=""/> <property name="user" value=""/> <property name="password" value=""/> </datasource> </environment> <-- To configure mapper.xml Mapping file--> <mappers> <mapper resource=""></mapper> </mappers> </environments>
2. Writing mapper.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"> <mapper namespace="org.lanqiao.entity.personMapper"> <select id="queryPersonById" resultType="org.lanqiao.entity.Person" parameterType="int"> select * from person where id = #{id} </select> </mapper>
mybatis specifies that an SQL statement can have at most one input parameter
3. Write the corresponding classes in the database
4. Write test classes (optional)
public static void main(String[] args) throws IOException { //Load the MyBatis configuration file (to access the database) Reader reader = Resources.getResourceAsReader("conf.xml") ; SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(reader) ; //session - connection SqlSession session = sessionFactory.openSession() ; //State is used to locate the SQL statement. It is made up of namespace.id in XXXmapper.xml. String statement = "org.lanqiao.entity.personMapper.queryPersonById" ; //When there are parameters, follow the state as the second parameter, and if there are no parameters, call the select function with only one parameter. Student person = session.selectOne( statement,1 ) ; System.out.println(person); session.close(); }
mybatis program upgrade
Writing a statement to locate SQL at a time is very troublesome. We can use dynamic proxy or interface development.
Differences from the first approach
Using the idea that convention is better than configuration, we omit statement and locate the SQL statement directly according to convention.
step
Con. xml, XXXmapper.xml and entity classes are all written in the same way as above.
The difference is to add an interface of XXXmapper.java.
Requirement:
1. The class name of mapper.java should be the same as the namespace in mapper.xml
2. The method name in mapper.java is the same as the id corresponding to each SQL statement in mapper.xml
3. The input parameter type of the method in mapper.java is the same as @parameterType in mapper.xml (without @parameterType, it is a parameterless function).
4. The return value type of the method in mapper.java is the same as @resultType in mapper.xml.
Then the test function can be modified to
StudentMapper studentMapper = session.getMapper(StudentMapper.class) ; studentMapper.Method();
Program optimization
Optimize 1: Put the database connection information in a configuration file db.properties separately
In order to view and modify the information about database connection, we can choose to put it in A. properties configuration file separately and express it by key-value pair.
Then, after introducing the. properties file into the conf.xml file, the variables and values in the conf.xml file are referenced in the form of ${}.
Example:
db.properties file:
driver=oracle.jdbc.OracleDriver url=jdbc:oracle:thin:@127.0.0.1:1521:ORCL username=scott password=tiger
conf.xml file:
<properties resource="db.properties"/> <dataSource type="POOLED"> <property name="driver" value="${driver}"/> <property name="url" value="${url}"/> <property name="username" value="${username}"/> <property name="password" value="${password}"/> <dataSource/>
Optimize 2: Set aliases
Write @resultType in the mapper.xml file if it's an object type.
For example, resultType="org.lanqiao.entity.Person"
By setting aliases, we can simplify this way of writing.
How to set aliases
In the conf.xml file:
<typeAliases> <- Set aliases individually - > <typeAlias type="org.lanqiao.entity.Person" alias="Person"> < -- Setting aliases in batches means that the aliases of all classes under this package are the class names after removing the package names--> <package name="org.lanqiao.entity"> <typeAliases>