Home>

When database fields and attributes in entity beans are inconsistent

The database person name field was name before, and the attribute in personbean was also name, but it was changed to user_name in the database.

Method 1:Alias ​​through the fields of the SQL statement,Object properties in aliases and entities are consistent

select id, user_name as name, sex, age from person
<select resulttype="com.luogg.domain.person">
  select id, user_name as name, sex, age from person
</select>

Method 2:The most powerful part of mybatis:reslutmap object

Add an intermediary resultmap tag, and change the resulttype in the select tag to resultmap, and correspond to the id of the resultmap tag.

Result set:resulttype base type,int, string, person,

resultmap exists for the mediation tag resultmap.

<!-Configure namespace,Namespace + .id is the only sql statement identifier->
<mapper namespace="com.luogg.mapper.personmapper">
 <!-Intermediary, when the database field and entity bean object attributes are inconsistent,Make a correspondence->
 <resultmap type="com.luogg.domain.person">
  <!-Primary key mapping->
  <id property="id" column="id"></id>
  <!-Ordinary field,property refers to the entity property,the column name of the column result set,Consistent fields can be left out->
  <result property="name" column="user_name"></result>
 </resultmap>
 <!-Query all data,The parameters are id, resulttype result set, parametertype parameters->
 <!-Note:If there is a set to be filled in the sql statement,Such as querying all data,Return the result set of a person,Then the resulttype parameter is directly written the type of path + collection
  For example:return a person collection, then fill in the path where the person bean is located + person->
 <select resultmap="personrm">
  select * from person
 </select>
 <!-Query data by id,When there are query conditions,Need to write parametertype, return result set is still person, #{id} or ${id} can be->
 <select parametertype="int" resulttype="com.luogg.domain.person">
  select * from person where id=#(id)
 </select>
</mapper>

mybatis

Add personnel information

First, add the insert tag in personmapper.xml. Our database field user_name and person entity attribute are name.Then write the code in testmybatis.

<!-Add person->
 <insert parametertype="com.luogg.domain.person">
  insert into person (id, user_name, age, sex) values ​​(#{id}, #{name}, #{age}, #{sex})
 </insert>

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 is thread-safe
 private sqlsessionfactory factory;
 @before
 public void init () throws ioexception {
  string resource="sqlmapconfig.xml";
  inputstream is=resources.getresourceasstream (resource);
  factory=new sqlsessionfactorybuilder (). build (is);
 }
 @test //Query all
 public void findall () throws ioexception {
  /**
   * Test database connection
   * 1. Define a variable resource of type string, pointing to the xml file of the connection database just configured
   * 2. Create an input stream,To read our database configuration file
   * 3. Input stream to create a factory.
   * 4. Open factory after having factory
   * 5. Access the sql statement in the configuration file through the session
   * /
  sqlsession session=factory.opensession ();
  //How to access the sql statement in personmapper.xml?namespace + .id
  list<person>list=session.selectlist ("com.luogg.mapper.personmapper.find");
  system.out.println (list.size ());
  for (person p:list) {
   system.out.println (p);
  }
 }
 @test //Query personnel information by id
 public void selbyid () {
  sqlsession session=factory.opensession ();
  person p=session.selectone ("com.luogg.mapper.personmapper.selbyid", 1);
  system.out.println (p);
 }
 @test //Add personnel information
 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 ("Successfully added personnel");
  }
 }
}

Modify personnel information, query the total number of records

prsonmapper.xml file

<!-Modify member information->
 <update parametertype="com.luogg.domain.person">
  update person set user_name=#{name}, age=#{age} where id=#{id}
 </update>
 <!-Query total records->
 <select resulttype="int">
  select count (*) from person
 </select>

testmybatis.java file

@test //modify personnel information
 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 ("Successfully modified information");
  }
 }
 @test //Query the total number of records
 public void selcount () {
  sqlsession session=factory.opensession ();
  int i=session.selectone ("com.luogg.mapper.personmapper.count");
  system.out.println (i);
 }

Conditional query

Search by criteria

<!-Query with conditions->
 <select parametertype="map" resultmap="personrm">
  select<include refid="cols" />from person
  where user_name like #{name}
  and sex=#{sex}
 </select>
@test //Query with conditions
 public void selbyl () {
  sqlsession session=factory.opensession ();
  map<string, object>map=new hashmap<string, object>();
  map.put ("name", "luo%");
  map.put ("sex", 1);
  list<person>list=session.selectlist ("com.luogg.mapper.personmapper.selbyl", map);
  system.out.println (list.size ());
  for (person p:list) {
   system.out.println (p);
  }
 }
  • Previous Visual Studio debugging tips summary
  • Next Detailed springmvc json data interaction controller method return value is simple type