一、引入mybatis及mysql的jar包
可以从阿里云上面查找版本,db操作放在dao层所以打开该层的pom.xml文件,找到
<dependency> <groupId>org.mybatisgroupId> <artifactId>mybatisartifactId> <version>3.4.5version> dependency> <dependency> <groupId>mysqlgroupId> <artifactId>mysql-connector-javaartifactId> <version>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=root
jdbc.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"> <configuration> <properties resource="mysql.properties">properties> <typeAliases> <package name="com.tyh.entity"/> typeAliases> <environments default="dev"> <environment id="dev"> <transactionManager type="JDBC"/> <dataSource type="POOLED"> <property name="driver" value="${jdbc.driver}"/> <property name="url" value="${jdbc.url}"/> <property name="username" value="${jdbc.username}"/> <property name="password" value="${jdbc.password}"/> dataSource> environment> environments> <mappers> <package name="com/tyh/dao/mapper"/> mappers> configuration>
4.在src/main/java下创建各自的包,我这里是com.tyh.dao.mapper,在mapper文件夹下创建UserMapper的接口文件,用于编写DB操作函数
public interface UserMapper { /** * 添加用户 * @param entity 实体 * @return 添加id * @throws Exception */ int add(UserEntity entity) throws Exception; int delete(int id) throws Exception; int update(UserEntity entity) throws Exception; UserEntity get(int id) throws Exception; Listlist() throws Exception; }
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"> <mapper namespace="com.tyh.dao.mapper.UserMapper"> <insert id="add" useGeneratedKeys="true" keyProperty="id"> insert into user (username, password, age) values (#{userName},#{password},#{age}); insert> <delete id="delete" parameterType="int"> delete from user where id=#{id} delete> <update id="update" > update user set username=#{username}, password=#{password}, age=#{age} where id=#{id}; update> <select id="get" resultType="com.tyh.entity.UserEntity"> select * from user where id=#{id}; select> <select id="list" resultType="com.tyh.entity.UserEntity"> select * from user; select> mapper>
6.我们在文件夹内添加的xml及properties文件默认编译不会被复制过去,所以运行时会提示找不到文件,在配置中指明要一起打包文件即可
我这个项目有parent模块,所以在父模块中添加配置,子模块会自动继承,如果没有父模块则单独在demo-dao层的pom.xml文件添加也可
<build> <resources> <resource> <directory>src/main/javadirectory> <includes> <include>**/*.xmlinclude> includes> resource> <resource> <directory>src/main/resourcesdirectory> <includes> <include>**/*.*include> includes> resource> resources> build>
7.基础已经准备好了,下面创建数据库Mybatis操作对象,在com.tyh.dao下创建DBTools数据库工具类
public class DBTools { public static SqlSessionFactory sessionFactory; static { try { //使用MyBatis提供的Resources类加载mybatis的配置文件 Reader reader = Resources.getResourceAsReader("mybatis_cfg.xml"); //构建sqlSession的工厂 sessionFactory = new SqlSessionFactoryBuilder().build(reader); } catch (Exception e) { e.printStackTrace(); } } //创建能执行映射文件中sql的sqlSession public static SqlSession getSqlSession(){ return sessionFactory.openSession(); } }
8.编写dao操作层,用于调用底层实现函数
public class UserDao { private SqlSession sqlSession; private UserMapper mapper; public UserDao() { sqlSession = DBTools.getSqlSession(); mapper = sqlSession.getMapper(UserMapper.class); } public int add(UserEntity entity) throws Exception { //调用数据库操作函数后需要commit才会提交 int result = mapper.add(entity); sqlSession.commit(); return result; } public int delete(int id) throws Exception { int result = mapper.delete(id); sqlSession.commit(); return result; } public int update(UserEntity entity) throws Exception { int result = mapper.update(entity); sqlSession.commit(); return result; } public UserEntity get(int id) throws Exception { UserEntity result = mapper.get(id); sqlSession.commit(); return result; } public Listlist() throws Exception { List result = mapper.list(); sqlSession.commit(); return result; } }
至此Dao层的DB操作已经完成,自己编写service和web调用即可,以下是我项目的结构。
注:数据库创建时也需要指明utf8格式编码,否则会出现中文乱码问题
create database test charset utf8;