码迷,mamicode.com
首页 > 其他好文 > 详细

mybatis基础

时间:2020-01-06 12:59:22      阅读:67      评论:0      收藏:0      [点我收藏+]

标签:更新   row   builder   查询   cal   date()   efault   引用   imp   

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基础

标签:更新   row   builder   查询   cal   date()   efault   引用   imp   

原文地址:https://www.cnblogs.com/fpflog/p/12155477.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!