首先新建一个mavan 项目 选择Createfrom archetype 选择archetype-webapp 创建
在pom.xml文件里配置需要的包
<dependencies>
<dependency>
<groupId>junitgroupId>
<artifactId>junitartifactId>
<version>3.8.1version>
<scope>testscope>
dependency>
<dependency>
<groupId>mysqlgroupId>
<artifactId>mysql-connector-javaartifactId>
<version>5.1.40version>
dependency>
<dependency>
<groupId>org.mybatisgroupId>
<artifactId>mybatisartifactId>
<version>3.4.2version>
dependency>
<dependency>
<groupId>javax.servletgroupId>
<artifactId>jstlartifactId>
<version>1.2version>
dependency>
<dependency>
<groupId>javax.elgroupId>
<artifactId>javax.el-apiartifactId>
<version>3.0.0version>
dependency>
<dependency>
<groupId>org.apache.commonsgroupId>
<artifactId>commons-lang3artifactId>
<version>3.4version>
dependency>
<dependency>
<groupId>net.sf.json-libgroupId>
<artifactId>json-libartifactId>
<version>2.4version>
<classifier>jdk15classifier>
dependency>
<dependency>
<groupId>com.github.pagehelpergroupId>
<artifactId>pagehelperartifactId>
<version>4.1.4version>
dependency>
dependencies>
创建mybatis-config.xml
<?xml version="1.0" encoding="UTF-8"?>
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
//配置文件
<properties resource="db.properties"/>
//起别名
<typeAliases>
<typeAlias type="com.oracle.mapper.Role" alias="role"/>
typeAliases>
<environments default="development">
<environment id="development">
<transactionManager type="JDBC"/>
dataSource标签
是配置数据源的链接信息 type属性是供我们对数据库连接方式的配置
UNPOOLED 非数据库连接池
POOLED 数据库连接池
JNDL 数据源
property标签是定义数据库的各类参数
<dataSource type="POOLED">
<property name="driver" value="${driver}"/>
<property name="url" value="${url}"/>
<property name="username" value="${username}"/>
<property name="password" value="${password}"/>
dataSource>
environment>
environments>
3mapper标签是引入映射器
注解:
XML:
<mappers>
<mapper resource="mapper/roleMapper.xml"/>
mappers>
configuration>
在resources创建数据库连接的配置文件db.properies
driver=com.mysql.jdbc.Driver
username=root
password=53542316
url=jdbc:mysql://localhost:3306/mybatis?useUnicode=true&tcharacterEncoding=UTF-8
在resources创建映射器mapper文件夹创建mapper的.xml文件
<?xml version="1.0" encoding="UTF-8"?>
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.oracle.mapper.RoleMapper">
<resultMap type="com.oracle.mapper.Role" id="Map">
<id column="id" property="id"/>
<result column="rolename" property="rolename"/>
<result column="note" property="note"/>
resultMap>
<select id="selectAll" resultMap="Map">
select * from t_role
select>
<select id="getRole"
parameterType="long"
resultType="role">
SELECT * FROM t_role WHERE id=#{id}
select>
<insert id="addRole"
parameterType="role" useGeneratedKeys="true" keyProperty="id">
INSERT into t_role(rolename,note) VALUES(#{rolename},#{note})
insert>
<select id="gRole" resultType="int" >
SELECT COUNT(*)FROM t_role
select>
mapper>
创建ManualExample
public class ManualExample {
public static voidmain(String[] args) {
//会话工厂
SqlSessionFactory sessionFactory=null;
//读取配置文件
String resource ="mybatis-config.xml";
InputStream inputStream =null;
try {
//从类路径中寻找文件
inputStream = Resources.getResourceAsStream(resource);
} catch (IOException e) {
e.printStackTrace();
}
sessionFactory =newSqlSessionFactoryBuilder().build(inputStream);
//从工厂中取一个会话, 类似于 Connection
SqlSession session = sessionFactory.openSession();
System.out.println(session);
//声明接口 session生成一个我们看不见的实现类
RoleMapper roleMapper = session.getMapper(RoleMapper.class);
Role role = roleMapper.getRole(1);
System.out.println(role.getRolename());
session.commit();
session.close();
}
}
在java包中创建接口类
public interface RoleMapper {
//接口名字和刚才的mapper.xml中定义的id要一致
public Role getRole(longl);
public void addRole(Role role);
public int gRole();
public List
}