mybatis基础

Posted fpflog

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了mybatis基础相关的知识,希望对你有一定的参考价值。

1、引入jar包

2、将 log4j.properties  copy到src --main --resources下

3、创建实体类

4、创建操作接口类

package com.fpf.dao;

import com.fpf.domain.User;

import java.util.List;

public interface UserDao {
/*

查询所用用户
*/
public List<User> findAll();
/*
* 保存用户
* */
public void save(User user);
/*
* 更新用户
* */
public void update(User user);
/*
* 删除用户
* */
public void delete(int id);
/*
* 模糊查询
* */
public List<User> selectLike(String uname);
}

5、在src --main --resources创建configuration.xml

其目的是配置数据库信息 ,引用mappers信息

<?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>
<environments default="mysql">
<environment id="mysql">
<transactionManager type="JDBC"></transactionManager>
<dataSource type="POOLED">
<property name="driver" value="com.mysql.jdbc.Driver"></property>
<property name="url" value="jdbc:mysql://localhost:3306/test"></property>
<property name="username" value="root"></property>
<property name="password" value="root"></property>
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource="com/fpf/Dao/UserDao.xml"></mapper>
</mappers>
</configuration>
6、配置mapper
将mapper放在src --main --resources 自建文件夹将xxxmapper.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.fpf.dao.UserDao">
<select id="findAll" resultType="com.fpf.domain.User">
SELECT * from user;
</select>
<insert id="save" parameterType="com.fpf.domain.User">
insert into user (username,password,name,sex,brithday) values(#{username},#{password},#{name},#{sex},#{brithday});
</insert>
<update id="update" parameterType="com.fpf.domain.User">
update user set username=#{username} where id=#{id};
</update>
<delete id="delete" parameterType="int">
delete from user where id=#{uid};
</delete>
<select id="selectLike" parameterType="String" resultType="com.fpf.domain.User">
select * from user where name like #{name};
</select>
</mapper>
注:
namespace对应所用类   id对应方法名称    parameterType对应出入数据类型 类型为自带类型#{任意定义} 类型为自定义类型类#{与实体类属性值对应}     resultType对应返回数据类型    
7、建立测试类
package com.fpf.test;

import com.fpf.dao.UserDao;
import com.fpf.domain.User;
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 org.junit.After;
import org.junit.Before;
import org.junit.Test;



import java.io.InputStream;
import java.util.Date;
import java.util.List;

public class UserTest {
InputStream in;
SqlSessionFactoryBuilder builder;
SqlSessionFactory factory;
SqlSession session;
UserDao mapper;
@Before
public void init()throws Exception{
//获取xml
in= Resources.getResourceAsStream("MySqlConfigration.xml");
//构建创建者
builder=new SqlSessionFactoryBuilder();
//构建工厂
factory= builder.build(in);
//创建对象
session = factory.openSession();
mapper = session.getMapper(UserDao.class);
}
@After
public void destroy() throws Exception{
session.commit();
session.close();
in.close();
}
@Test
public void findAllTest() {
List<User> all = mapper.findAll();
for (User user : all) {
System.out.println(user);
}

}
@Test
public void saveTest(){
User user=new User();
user.setUsername("zhangliang");
user.setPassword("123456");
user.setName("张良");
user.setSex("男");
user.setBrithday(new Date());
mapper.save(user);
}
@Test
public void updateTest(){
User user=new User();
user.setId(2);
user.setUsername("zl");
mapper.update(user);
}
@Test
public void deleteTest(){
mapper.delete(2);
}
@Test
public void selectLikeTest(){
List<User> users = mapper.selectLike("%小%");
for (User user : users) {
System.out.println(user);
}

}

}

以上是关于mybatis基础的主要内容,如果未能解决你的问题,请参考以下文章

mybatis学习(39):动态sql片段

SSM-MyBatis-05:Mybatis中别名,sql片段和模糊查询加getMapper

mybatis动态sql片段与分页,排序,传参的使用

MyBatis动态SQL标签用法

MYBATIS05_ifwherechoosewhentrimsetforEach标签sql片段

[vscode]--HTML代码片段(基础版,reactvuejquery)