一、引入mybatis及mysql的jar包
可以从阿里云上面查找版本,db操作放在dao层所以打开该层的pom.xml文件,找到节点增加两个引入
org.mybatisgroupId>
mybatisartifactId>
3.4.5version>
dependency>
mysqlgroupId>
mysql-connector-javaartifactId>
5.1.45version>
dependency>
保存后系统会自动下载对应版本的jar包,我们开始编码
二、配置mybatis(手动创建)
1.在dao层的src/main下创建和java文件夹同级目录的resources文件夹,它默认会变换类型,如果不变则手动调整一下
2.在resources文件夹下创建mysql.properties文件
填入mysql数据库连接信息
jdbc.driver=com.mysql.jdbc.Driver#数据库连接允许中文需要指明编码方式
jdbc.url=jdbc:mysql://10.11.12.237:3306/tyh_test?useUnicode=true&characterEncoding=utf8
jdbc.username=rootjdbc.password=root
3.在resources文件夹下创建mybatis_cfg.xml文件
填入mybatis配置信息
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">
properties>
typeAliases>
dataSource>
environment>
environments>
mappers>
configuration>
4.在src/main/java下创建各自的包,我这里是com.tyh.dao.mapper,在mapper文件夹下创建UserMapper的接口文件,用于编写DB操作函数
public interfaceUserMapper {/*** 添加用户
*@paramentity 实体
*@return添加id
*@throwsException*/
int add(UserEntity entity) throwsException;int delete(int id) throwsException;int update(UserEntity entity) throwsException;
UserEntity get(int id) throwsException;
Listlist() throwsException;
}
5.同样在mapper文件夹下创建UserMapper.xml文件,用于编写与接口函数对应的SQL脚本,此处切记节点属性不要写错,否则会报错
xml version="1.0" encoding="UTF-8"?>
DOCTYPE mapper PUBLIC "-//mybatis.org/DTD Mapper 3.0" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
insert into user (username, password, age) values (#{userName},#{password},#{age});insert>
delete from user where id=#{id}delete>
update user set username=#{username}, password=#{password}, age=#{age} where id=#{id};update>
select * from user where id=#{id};select>
select * from user;select>
mapper>
6.我们在文件夹内添加的xml及properties文件默认编译不会被复制过去,所以运行时会提示找不到文件,在配置中指明要一起打包文件即可
我这个项目有parent模块,所以在父模块中添加配置,子模块会自动继承,如果没有父模块则单独在demo-dao层的pom.xml文件添加也可
src/main/javadirectory>
**/*.xmlinclude>
includes>
resource>
src/main/resourcesdirectory>
**/*.*include>
includes>
resource>
resources>
build>
7.基础已经准备好了,下面创建数据库Mybatis操作对象,在com.tyh.dao下创建DBTools数据库工具类
public classDBTools {public staticSqlSessionFactory sessionFactory;static{try{//使用MyBatis提供的Resources类加载mybatis的配置文件
Reader reader = Resources.getResourceAsReader("mybatis_cfg.xml");//构建sqlSession的工厂
sessionFactory = newSqlSessionFactoryBuilder().build(reader);
}catch(Exception e) {
e.printStackTrace();
}
}//创建能执行映射文件中sql的sqlSession
public staticSqlSession getSqlSession(){returnsessionFactory.openSession();
}
}
8.编写dao操作层,用于调用底层实现函数
public classUserDao {privateSqlSession sqlSession;privateUserMapper mapper;publicUserDao() {
sqlSession=DBTools.getSqlSession();
mapper= sqlSession.getMapper(UserMapper.class);
}public int add(UserEntity entity) throwsException {//调用数据库操作函数后需要commit才会提交
int result =mapper.add(entity);
sqlSession.commit();returnresult;
}public int delete(int id) throwsException {int result =mapper.delete(id);
sqlSession.commit();returnresult;
}public int update(UserEntity entity) throwsException {int result =mapper.update(entity);
sqlSession.commit();returnresult;
}public UserEntity get(int id) throwsException {
UserEntity result=mapper.get(id);
sqlSession.commit();returnresult;
}public Listlist() throwsException {
Listresult =mapper.list();
sqlSession.commit();returnresult;
}
}
至此Dao层的DB操作已经完成,自己编写service和web调用即可,以下是我项目的结构。
注:数据库创建时也需要指明utf8格式编码,否则会出现中文乱码问题
create database test charset utf8;