Mybatis
作者:互联网
关于Mybatis的简单介绍:myBatis是一个用于作用于数据库的
mybatis的配置说明:
这个项目需要创建的文件:
导入依赖:
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
</dependency>
<!-- 设置好数据库的引用和mybatis引用,使用的是本地仓库的缓存的包 -->
<dependency>
<groupId>com.oracle</groupId>
<artifactId>ojdbc6</artifactId>
<version>${ojdbc.version}</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>${mybatis.version}</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>${lombok.version}</version>
</dependency>
<!-- 分页插件导入 -->
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper</artifactId>
<version>4.1.4</version>
</dependency>
<!-- 导入jstl的标签依赖包 -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<!-- 导入servlet的依赖包 -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.0.1</version>
<scope>provided</scope>
</dependency>
<!-- 导入servlet.jsp的依赖包 -->
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>jsp-api</artifactId>
<version>2.2</version>
<scope>provided</scope>
</dependency>
</dependencies>
配置mysql的全局配置config.xml:
<?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>
<settings>
<!--是否开启全局二级缓存-->
<setting name="cacheEnabled" value="true"/>
<!-- 是否在控制台打印日志 -->
<setting name="logImpl" value="STDOUT_LOGGING" />
</settings>
<!--类的别名配置区域-->
<!-- 别名 -->
<typeAliases>
<typeAlias type="com.sc.pojo.Emp" alias="emp" />
<typeAlias type="com.sc.pojo.News" alias="news" />
<typeAlias type="com.sc.pojo.Theme" alias="theme" />
</typeAliases>
<!--分页插件-->
<plugins>
<plugin interceptor="com.github.pagehelper.PageHelper">
<property name="dialect" value="oracle"/>
<property name="offsetAsPageNum" value="false"/>
<property name="rowBoundsWithCount" value="false"/>
<property name="pageSizeZero" value="true"/>
<property name="reasonable" value="false"/>
<property name="supportMethodsArguments" value="false"/>
<property name="returnPageInfo" value="none"/>
</plugin>
</plugins>
<!-- 配置默认的数据库连接 -->
<!-- 配置好数据库的执行引擎以及账号密码 -->
<environments default="oracledb">
<!-- 配置数据库连接信息 -->
<environment id="oracledb">
<transactionManager type="jdbc"></transactionManager>
<dataSource type="pooled">
<property name="driver" value="oracle.jdbc.driver.OracleDriver"/>
<property name="url" value="jdbc:oracle:thin:@localhost:1521:XE"/>
<property name="username" value="scott"/>
<property name="password" value="123"/>
</dataSource>
</environment>
<!-- 配置数据库连接信息 -->
<environment id="mysqldb">
<transactionManager type="jdbc"></transactionManager>
<dataSource type="pooled">
<property name="driver" value="oracle.jdbc.driver.OracleDriver"/>
<property name="url" value="jdbc:oracle:thin:@localhost:1521:orcl"/>
<property name="username" value="scott"/>
<property name="password" value="tiger"/>
</dataSource>
</environment>
</environments>
<!--配置映射文件-->
<mappers>
<mapper resource="mapper/EmpMapper.xml"></mapper>
<mapper resource="mapper/NewsMapper.xml"></mapper>
<mapper resource="mapper/ThemeMapper.xml"></mapper>
</mappers>
</configuration>
关于映射mapper.xml的配置:
<?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.sc.dao.IEmpDao">
<cache></cache> <!--用于加入二级缓存 -->
<resultMap id="getEmpListMap" type="emp" >
<id property="no" column="empno" />
<result column="ename" property="name" />
</resultMap>
<select id="getEmpList" resultMap="getEmpListMap">
select * from emp
</select>
<select id="getEmpById" resultMap="getEmpListMap">
select * from emp where empno=#{no}
</select>
<select id="getEmpByIdName" resultMap="getEmpListMap">
select * from emp where empno=#{id} and ename = #{name}
</select>
<insert id="insertEmp">
insert into emp(empno,ename,job,sal,hiredate)
values(#{id},#{name},#{job},#{sal},#{time})
</insert>
<update id="UpdateEmpById" >
update emp set ename=#{name} where empno = #{id}
</update>
<delete id="delEmpById">
delete from emp where empno=#{id}
</delete>
<!-- 动态查询中,大于和小于使用转义字符
大于:> > 小于:< <
大于等于:>= ≥ 小于等于:<= ≤
模糊查询: 连接字符串使用concat();
-->
<select id="getEmpByIdJob" resultMap="getEmpListMap">
select * from emp where 1=1
<if test="id > 0">
and empno = #{id}
</if>
<if test="job != null and job != ''">
and job like concat('%',concat(#{job},'%'))
</if>
</select>
<delete id="delEmpByIds" >
delete from emp2 where empno in
<foreach collection="list" open="("
close=")" item="empno" separator=",">
#{empno}
</foreach>
</delete>
</mapper>
关于dao的接口的配置:
public interface IEmpDao {
/**
*这里每一个方法名对应mapper.xml中的id
*/
List<Emp> getEmpList(); // 获取到所有的信息
Emp getEmpById(int id); // 根据ID获取信息
Emp getEmpByIdName(@Param("id") int id, @Param("name") String name); // 根据ID和姓名获取对象
int delEmpById(int id); //根据id删除对象
int UpdateEmpById(@Param("id") int id,@Param("name")String name); // 根据姓名进行更新对象
int insertEmp(@Param("id") int id, @Param("name") String name, @Param("sal")String sal, @Param("job")String job,
@Param("time") Date time); // 插入信息
Emp getEmpByIdJob(@Param("id")int id,@Param("job")String job); // 动态查询
int delEmpByIds(List<Integer> empnoList); // 批量删除
}
关于数据库字段与类对应的属性显示:
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.sql.Date;
import java.sql.Timestamp;
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Emp {
private int no;
private String name;
private String sal;
private String job;
private Date hiredate;
}
关于Mybatis的工厂的打开:
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.Reader;
public class SqlSessionUtil {
static SqlSessionFactory sqlSessionFactory;
static{
try {
Reader reader = Resources.getResourceAsReader("mybatis-config.xml");
sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
} catch (IOException e) {
e.printStackTrace();
}
}
public static SqlSession openSqlSession(){
return sqlSessionFactory.openSession();
}
}
关于mybatis的使用,使用了反射:
public class EmpText {
@Test
public void test1(){
SqlSession sqlSession = SqlSessionUtil.openSqlSession();
IEmpDao empDao = sqlSession.getMapper(IEmpDao.class);
List<Emp> list = empDao.getEmpList();
for (Emp emp : list){
System.out.println(emp);
}
}
@Test
public void test2(){
SqlSession sqlSession = SqlSessionUtil.openSqlSession();
IEmpDao empDao = sqlSession.getMapper(IEmpDao.class);
Emp emp = empDao.getEmpById(7521);
System.out.println(emp);
}
@Test
public void test3(){
SqlSession sqlSession = SqlSessionUtil.openSqlSession();
IEmpDao empDao = sqlSession.getMapper(IEmpDao.class);
Emp emp = empDao.getEmpByIdName(7521,"WARD");
System.out.println(emp);
}
@Test
public void test4(){
SqlSession sqlSession = SqlSessionUtil.openSqlSession();
IEmpDao empDao = sqlSession.getMapper(IEmpDao.class);
int count= empDao.insertEmp(7562,"WARD","75610","SALESMAN",new Date(15));
sqlSession.commit();
System.out.println(count);
}
@Test
public void test5(){
SqlSession sqlSession = SqlSessionUtil.openSqlSession();
IEmpDao empDao = sqlSession.getMapper(IEmpDao.class);
int count = empDao.UpdateEmpById(7562,"jiem");
System.out.println(count);
}
@Test
public void test6(){
SqlSession sqlSession = SqlSessionUtil.openSqlSession();
IEmpDao empDao = sqlSession.getMapper(IEmpDao.class);
int count = empDao.delEmpById(7562);
sqlSession.commit();
System.out.println(count);
}
/*动态查询*/
@Test
public void test7(){
SqlSession sqlSession = SqlSessionUtil.openSqlSession();
IEmpDao empDao = sqlSession.getMapper(IEmpDao.class);
Emp emp = empDao.getEmpByIdJob(7521,"S");
System.out.println(emp);
}
/*批量删除*/
@Test
public void test8(){
SqlSession sqlSession = SqlSessionUtil.openSqlSession();
IEmpDao empDao = sqlSession.getMapper(IEmpDao.class);
List list = new ArrayList();
list.add(7521);
list.add(7566);
int count = empDao.delEmpByIds(list);
sqlSession.commit();
System.out.println(count);
}
}
标签:int,empDao,IEmpDao,sqlSession,emp,Mybatis,id 来源: https://blog.csdn.net/qq_44969215/article/details/111773927