The first project MyBatis
Basic project
Import jar package
In addition we need to import MyBatis core Jar Jar package and dependent packages, but also need to import the MySql drivers Jar package, JUnit test Jar package.
MyBatis core Jar package and dependence Jar package, both the frame in MyBatis extract the directory. The specific tutorials and download the download link blogger has been integrated in * "Introduction to MyBatis download and" this blog, readers may need to check the option to download.
Bloggers ancient version 3.2.7 of your presentation here, but for demonstration with other versions of the same.
Generating a table structure in the database
Bloggers established here film table, below is a specific table structure:
Defined entity classes
public class Film {
private Integer id;
private String name;
private double price;
/*Constructors*/
/*Setter and Getter*/
/*toString*/
}
Defined interfaces
public interface IFilmDao {
void insertFilm(Film film);
}
Define the mapping file
Mapping file, commonly referred Mapper, Dao layer is mainly used to map SQL statement. Dao mapping files are generally placed under the package, so the file name and the corresponding need Dao layer interface name the same, so here we mapper file name is called IFilmDao.xml .
Constraints file mapping files normally stored org.apache.Ibatis.builder.xml package to MyBatis core jar package in the store. Of course, the main constraint file configuration files are stored here.
Add in the mapping file constraint that the head profile can be found from the MyBatis framework document mybatis-xxxpdf in:
bloggers here have been extracted from the document, you can directly use the copy:
<?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="com.mybatis.dao.IFilmDao">
<insert id="insertFilm" parameterType="com.mybatis.beans.Film">
insert into film(name, price)
values (#{name}, #{price});
</insert>
</mapper>
# {} Is the attribute name written Film type parameters
noted here that the value of each attribute:
namespace : Because we mapper file stored in the dao package, therefore must be fully qualified namespace mapped interface.
ID : the interface must be the name of the corresponding method.
parameterType : parameter type, the frame is automatically detected automatically SqlSession method performed by the user according to the parameters, we do not generally attribute specified parameterType
The definition of the main configuration file
Mapping and configuration files, the main constraint configuration file can also be found in the documentation mybatis-xxxpdf in MyBatis framework.
Here the main configuration file name can easily play, blogger called here mybatis.xml, position in the project root directory.
<?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">
<configuration>
<!-- 和spring整合后 environments配置将废除-->
<environments default="development">
<environment id="development">
<!-- 事务管理-->
<transactionManager type="JDBC"/>
<!-- 数据库连接池-->
<dataSource type="POOLED">
<property name="driver" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/mybatis?characterEncoding=utf-8"/>
<property name="username" value="root"/>
<property name="password" value="password"/>
</dataSource>
</environment>
</environments>
<!--注册映射文件-->
<mappers>
<mapper resource="com/mybatis/dao/IFilmDao.xml"/>
<!--<mapper package="com.mybatis.dao"/>-->
</mappers>
</configuration>
Dao defined layer implementation
/*imports*/
public class FilmDaoImpl implements IFilmDao {
private SqlSession sqlSession;
@Override
public void insertFilm(Film film) {
try {
//读取配置文件
InputStream inputStream = Resources.getResourceAsStream("mybatis.xml");
//创建SqlSessionFactory对象
SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
//创建SqlSession对象
sqlSession = sessionFactory.openSession();
//操作
sqlSession.insert("insertFilm", film);
//提交事务
sqlSession.commit();
} catch (IOException ex) {
ex.printStackTrace();
} finally {
if (sqlSession != null)
sqlSession.close();
}
}
}
Defines the test class
public class Test {
public void test01() {
IFilmDao dao = new FilmDaoImpl();
Film film = new Film("阿拉丁", 29.5);
dao.insertFilm(film);
}
}
Optimized Configuration and project
Our main configuration file name can be named, mainly to complete the following functions:
- Register file storage DB connection properties of the four elements
we can create jdbc.properties properties file in mybatis.xml same path.
In the main configuration file so that you can directly register the profile and connection data pool.jdbc.driver=com.mysql.jdbc.Driver jdbc.url=jdbc:mysql://127.0.0.1:3306/mybatis jdbc.user=root jdbc.password=password
- Alias the fully qualified class name of the entity class registration
When implemented as a SQL statement in the mapping file, accepts the parameters of the type of return that we can directly use the lowercase class name instead of the fully qualified name of a lengthy.<typeAliases> <package name="com.mybatis.beans"/> </typeAliases>
- MyBatis configuration of the operating environment, namely, data sources and transaction manager
- Sign mapping file
when we register multiple map files, you can use the package to select a registration packet, inside the automatic scanning mybatis xml mapping file will be individually registered.
The optimized master configuration file
<?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">
<configuration>
<!--注册属性文件-->
<properties resource="jdbc.properties"/>
<!--注册别名-->
<typeAliases>
<package name="com.mybatis.beans"/>
</typeAliases>
<!-- 和spring整合后 environments配置将废除-->
<environments default="development">
<environment id="development">
<!-- 事务管理-->
<transactionManager type="JDBC"/>
<!-- 数据库连接池-->
<dataSource type="POOLED">
<property name="driver" value="${jdbc.driver}"/>
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${jdbc.user}"/>
<property name="password" value="${jdbc.password}"/>
</dataSource>
</environment>
</environments>
<!--注册映射文件-->
<mappers>
<!--<mapper resource="com/mybatis/dao/IFilmDao.xml"/>-->
<!--多个映射文件时-->
<mapper package="com.mybatis.dao"/>
</mappers>
</configuration>
Mapping file
Corresponding mapping file will be simplified in this way:
<?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="com.mybatis.dao.IFilmDao">
<insert id="insertFilm" parameterType="film">
insert into film(name, price)
values (#{name}, #{price});
</insert>
</mapper>