这篇文章主要介绍了MyBatis入门之增删改查+数据库字段和实体字段不一致问题处理方法,需要的朋友可以参考下
当数据库字段和实体bean中属性不一致时
之前数据库Person名字字段是name,PersonBean中属性也是name,但是之后数据库中修改为了user_name,
方法1:通过sql语句的字段起别名,别名和实体中的对象属性一致SELECT id,user_name as name,sex,age from person
SELECT id,user_name as name,sex,age from person
方法2: mybatis最强大的地方 : reslutMap对象
添加一个中介reslutMap标签,并将select标签中的resultType改为resultMap,并对应resultMap标签的id.
结果集:resultType 基础类型,int,string,Person,
resultMap 针对中介标签resultMap而存在.
SELECT * from person
SELECT * FROM person WHERE id = #{id}
mybatis的增删改查
新增人员信息
首先在PersonMapper.xml中添加insert标签,我们的数据库字段user_name,Person实体属性为name,不一致,区别开来.然后在TestMybatis中写入代码.
INSERT INTO person(id,user_name,age,sex) VALUES(#{id},#{name},#{age},#{sex})
package test;import com.luogg.domain.Person;
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.Before;
import org.junit.Test;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
/**
* Created by luogg on 2017/2/17.
*/
public class TestMybatis {
//SqlSessionFactory为线程安全的
private SqlSessionFactory factory;
@Before
public void init() throws IOException {
String resource = "sqlMapConfig.xml";
InputStream is = Resources.getResourceAsStream(resource);
factory = new SqlSessionFactoryBuilder().build(is);
}
@Test //查询所有
public void findAll() throws IOException {
/**
* 测试数据库的连接
* 1.定义一个String类型的变量resource,指向刚才配置的连接数据库的xml文件
* 2.创建一个输入流,来读取我们的数据库配置文件
* 3.输入流创建工厂.
* 4.有了工厂之后open工厂
* 5.通过session访问配置文件中的sql语句
*/
SqlSession session = factory.openSession();
//如何访问PersonMapper.xml中的sql语句呢? 命名空间+ .id
List list = session.selectList("com.luogg.mapper.PersonMapper.find");
System.out.println(list.size());
for(Person p : list){
System.out.println(p);
}
}
@Test //通过ID查询人员信息
public void selById(){
SqlSession session = factory.openSession();
Person p = session.selectOne("com.luogg.mapper.PersonMapper.selById",1);
System.out.println(p);
}
@Test //添加人员信息
public void add(){
SqlSession session = factory.openSession();
Person p = new Person();
p.setId(4);
p.setName("luogg");
p.setAge(22);
p.setSex(1);
int i = session.insert("com.luogg.mapper.PersonMapper.insert",p);
session.commit();
if(i==1){
System.out.print("添加人员成功");
}
}
}
修改人员信息 , 查询总的记录条数
prsonMapper.xml文件
UPDATE person SET user_name=#{name},age=#{age} WHERE id=#{id}
SELECT COUNT(*) FROM person
TestMybatis.java文件@Test //修改人员信息
public void updatePer(){
SqlSession session = factory.openSession();
Person p = new Person();
p.setId(4);
p.setName("luoggg");
//p.setSex(1);
p.setAge(23);
int i = session.update("com.luogg.mapper.PersonMapper.update",p);
session.commit();
if(i==1){
System.out.print("修改信息成功");
}
}
@Test //查询总的记录条数
public void selCount(){
SqlSession session = factory.openSession();
int i = session.selectOne("com.luogg.mapper.PersonMapper.count");
System.out.println(i);
}
条件查询
根据条件查询
SELECT FROM person
where user_name like #{name}
AND sex=#{sex}
@Test //带条 件查询
public void selByL(){
SqlSession session = factory.openSession();
Map map = new HashMap();
map.put("name","luo%");
map.put("sex",1);
List list = session.selectList("com.luogg.mapper.PersonMapper.selByL",map);
System.out.println(list.size());
for(Person p : list){
System.out.println(p);
}
}
【相关推荐】