MyBatis framework of the preparation and set up the configuration file, SQL mapping file

1, enter the download address https://github.com/mybatis download MyBatis framework document

2. Create a web project in SMBMS1 MyEclipse

3, copy and paste the following three files to the bin directory under WEB-INF next web project

wpsF6A3.tmp

4, by introducing just copy MyEclipse project into three packets which

Right after the name of the selected item, select Build Path, right, select Configuer Build Path, select add JARS, select the bin directory under WEB-INF files under the project OK, so I added in the project over the jar file

5, to facilitate learning, the source file is imported mybatis-3.2.2.jar

Select Referenced Library following mybatis-3.2.2.jar right, select properties, select the java source attachment, if the file in workspase, you can choose workspase location, do not select the external location, if the file is unzipped on the choice of external folder, do not unzip on the choice of external file

6, the configuration of the relevant configuration file

New source folder in the following items (Why build source folder, because only he can be compiled), directory database configuration files are copied in, and creates a new file name for a file mybatis-config.xml, then introduced log4j.properties file

Open mybatis-config.xml configure

The mybatis manual copy and paste the following code came as the file header

<?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">

Follows after the introduction of profile head

<! - this is done by the configuration file mybatis connection to the database ->

<configuration>

<! - introduced database.properties File ->

<properties resource="database.properties"/>

<! - Configure mybatis the log is implemented as LOG4J ->

<settings>

<setting name="logImpl" value="LOG4J" />

</settings>

<environments default="development">

<environment id="development">

<! - Configure transaction management, transaction management using JDBC ->

<transactionManager type="JDBC"></transactionManager>

<- POOLED:! Mybatis own data sources, JNDI: based tomcat data source ->

<dataSource type="POOLED">

<property name="driver" value="${driver}"/>

<property name="url" value="${url}"/>

<property name="username" value="${user}"/>

<property name="password" value="${password}"/>

</dataSource>

</environment>

</environments>

<! - The mapper file is added to the configuration file ->

<mappers>

<mapper resource="cn/smbms/dao/user/UserMapper.xml"/>

</mappers>

</configuration>

When the addition profile, configuration code for each file required by the order of the front and rear, can enter the design window, select the add child to select the right configuration profile corresponding to the tag names need to be added

6, based on the following two packages cn.smbms.pojo src and cn.smbms.dao.user, create a User class cn.smbms.pojo package below, establish UserMapper.xml at cn.smbms.dao.user, open UserMapper.xml configuration file copy and paste the code from the manual head

<?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">

And configure the following code

<mapper namespace="cn.smbms.dao.user.UserMapper">

<! - the number of records the query the user table ->

<select id="count" resultType="int">

select count(1) as count from smbms_user

</select>

</mapper>

Carrying profile in order to ensure the time code is correct, it is best to have automatically prompt, the corresponding configuration file may be introduced dtd files, such as the introduction mapper.dtd file, first locate the file, the file in which mybatis-3.2.2.jar after extracting the mybatis-3.2.2.jar find mybatis-3.2.2-org-apache-ibatis-builder-xml document which has a corresponding mybatis-3-mapper.dtd, the header file - // mybatis .org // DTD Mapper 3.0 // EN copied into MyEclipse select window-preferences, input xml search, select xml Catalog, and then select the user specified entries, select the file system at LOCATION, select mybatis-3-mapper.dtd determined, enter key at - // mybatis.org//DTD Mapper 3.0 // EN, OK

7, create a test class

Project which joined JUnit4, first establish a source folder named test under the project, select the project name -new-other- input ju search -junit test case, input cn.smbms.dao.user in the package, enter UserMapperTest in the Name, finish - check junit4-OK

Enter the following code in UserMapperTest

package cn.smbms.dao.user;

import static org.junit.Assert.*;

import java.io.IOException;

import java.io.InputStream;

import org.apache.ibatis.io.Resources;

import org.apache.ibatis.session.SqlSession;

import org.apache.ibatis.session.SqlSessionFactory;

import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import org.apache.log4j.Logger;

import org.junit.Before;

import org.junit.Test;

public class UserMapperTest {

private Logger logger = Logger.getLogger(UserMapperTest.class);

@Before

public void setUp() throws Exception {

}

@Test

public void test() {

String resource = "mybatis-config.xml";

int count = 0;

SqlSession sqlSession = null;

try {

// Get an input stream mybatis-config.xml

InputStream is = Resources.getResourceAsStream(resource);

// create a SqlSessionFactory 2, complete reading of the configuration file

SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(is);

// create sqlSession 3

sqlSession = factory.openSession();

4 // call mapper to operate the data file, the file must be first introduced into the mapper in mybatis-config.xml

count = sqlSession.selectOne("cn.smbms.dao.user.UserMapper.count");

logger.debug("UserMapperTest count---> " + count);

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace ();

}finally{

sqlSession.close();

}

}

}

When finished, right-Run as-JUnit test, and then view the results in the consol

[DEBUG] 2019-10-06 12:05:56,860 cn.smbms.dao.user.UserMapperTest - UserMapperTest count---> 12

Guess you like

Origin www.cnblogs.com/pere/p/11626954.html