Create a new web project in the Idea
Ready to work
Design of the good in the database table flower
New Flower entity class, containing id, name, price, production properties
Create a global configuration file in the src directory ***. Xml
Introduced in the xml file DTD or schema
to add profile content
<?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>
<!--default 引用environment的id,当前所使用的环境-->
<environments default="default">
<!--声明可以使用的环境-->
<environment id="default">
<!--使用原生JDBC事务-->
<transactionManager type="JDBC"></transactionManager>
<dataSource type="POOLED">
<property name="driver" value="com.mysql.jdbc.Driver"></property>
<property name="url" value="jdbc:mysql://localhost:3306/ssmdemo"></property>
<property name="username" value="root"></property>
<property name="password" value="123"></property>
</dataSource>
</environment>
</environments>
<!--映射的mapper文件-->
<mappers>
<mapper resource="com/test/mapper/FlowerMapper.xml"></mapper>
</mappers>
</configuration>
<dataSource>
In type
property:
1.POOLED use the database connection pool
2.UNPOOLED does not use a database connection pool, with native JDBC as
3.JNDI java directory named technical interfaces
purpose of using the database connection pool server system is to reduce stress, increase process efficiency, in memory opened up a space for storing a plurality of database connection object, with the idle state is divided into active, small projects may not be used.
Import dependent jar package
In the web / WEB-INF lib new folder storage jar package, adding the jar package projectStructure-> Module in
Ending in a new packet mapper
mapper packet as the data access layer, i.e., layer dao
the xml file is implemented as a class
<?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.test.mapper1">
<!--id:方法名
parameterType: 定义参数类型
resultType: 返回值类型
如果方法返回值是 list,在 resultType 中写 List 的泛型,
因为 mybatis 对 jdbc 封装,一行一行读取数据 -->
<select id="selAll" resultType="com.test.POJO.Flower">
select * from flower
</select>
</mapper>
Write test code
package com.test.test;
import com.test.POJO.Flower;
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 java.io.IOException;
import java.io.InputStream;
import java.util.List;
public class Test {
public static void main(String[] args) throws IOException {
InputStream is = Resources.getResourceAsStream("mybatis.xml");
// 使用工厂模式
SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(is);
SqlSession sqlSession = factory.openSession();
List<Flower> list = sqlSession.selectList("com.test.mapper1.selAll");
for (Flower flower:list) {
System.out.println(flower.toString());
}
sqlSession.close();
}
}
Implement database connection pool JDBC Tomcat Pool
Under the new web META-INF folder, New context.xml
<?xml version="1.0" encoding="UTF-8" ?>
<Context>
<Resource
driverClassName="com.mysql.jdbc.Driver"
url="jdbc:mysql://localhost:3306/ssmdemo"
username="root"
password="123"
maxActive="100"
maxIdle="20"
// 此为连接池名称
name="test"
auth="Container"
maxWait="10000"
type="javax.sql.DataSource"
/>
</Context>
Calls to the database connection pool in the servlet
Context cxt = new InitialContext();
DataSource ds = (DataSource) cxt.lookup("java:comp/env/test");
Connection conn = ds.getConnection();
mybatis three ways to search
1.selectList()
List<Flower> list = session.selectList("a.b.selAll");
for (Flower flower : list) {
System.out.println(flower.toString());
}
2.selectOne()
int count = session.selectOne("a.b.selById");
System.out.println(count);
3.selectMap ()
behind the parameter as the key column name
Map<Object, Object> map = session.selectMap("a.b.c", "name123");
System.out.println(map);