Mybaits入门使用

网友投稿 279 2022-09-04

Mybaits入门使用

1.pom.xml配置信息

junit junit 4.12 provided org.mybatis mybatis 3.1.1 mysql mysql-connector-java 5.1.38

测试类:

public class Person { private int id; private String name; private int age; private String address; private String birthday; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public String getAddress() { return address; } public void setAddress(String address) { this.address = address; } public String getBirthday() { return birthday; } public void setBirthday(String birthday) { this.birthday = birthday; } @Override public String toString() { return "Person [id=" + id + ", name=" + name + ", age=" + age + ", address=" + address + ", birthday=" + birthday + "]"; }}

sql语句xml配置:

SELECT LAST_INSERT_ID() insert into Person(name,age,address,birthday) value(#{name},#{age},#{address},#{birthday}) delete from Person where id = #{id} update Person set name=#{name},age=#{age},birthday=#{birthday},address=#{address} where id = #{id}

数据库访问配置:

TestMybatis:

import java.io.IOException;import java.io.InputStream;import java.util.List;import com.sun.org.apache.xpath.internal.SourceTree;import com.youfan.entity.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.Test;import java.io.InputStream;public class TestMybatis { public SqlSessionFactory getfactory() throws IOException { String filepath="SqlMappingConfig.xml"; InputStream in = Resources.getResourceAsStream(filepath); SqlSessionFactory sqlSessionFactory=new SqlSessionFactoryBuilder().build(in); return sqlSessionFactory; } @Test public void testinsert() throws IOException { SqlSessionFactory sqlSessionFactory = this.getfactory(); SqlSession sqlsession = sqlSessionFactory.openSession(); Person person = new Person(); person.setName("小高"); person.setAddress("上海"); person.setAge(15); person.setBirthday("05-04"); sqlsession.insert("test1.inserperson", person); System.out.println("id "+person.getId()); sqlsession.commit(); sqlsession.close(); } @Test public void testquerybyid() throws IOException{ SqlSessionFactory sqlSessionFactory = this.getfactory(); SqlSession sqlsession = sqlSessionFactory.openSession(); Person person = sqlsession.selectOne("querypersonbyid", 6); System.out.println(person); sqlsession.close(); } @Test public void testquerybyname() throws IOException{ SqlSessionFactory sqlSessionFactory = this.getfactory(); SqlSession sqlsession = sqlSessionFactory.openSession(); List personlist = sqlsession.selectList("querypersonbyname", "小高"); for(int i=0;i

表字段:

用映射方法写:

mapper:

public interface PersonMapper { public Person querypersonbyid(int id); public List querypersonbyname(String name); public void inserperson(Person person); public void deletepersonbyid(int id); public void updatepersonbyid(Person person); public List querypersonbyvo(PersonVo personVo);}

视图对象vo:

public class CustomPerson extends Person{}

public class PersonVo { private CustomPerson customPerson; public CustomPerson getCustomPerson() { return customPerson; } public void setCustomPerson(CustomPerson customPerson) { this.customPerson = customPerson; } }

语句:

and name = "${customPerson.name}" and birthday = "${customPerson.birthday}" SELECT LAST_INSERT_ID() insert into Person(name,age,address,birthday) value(#{name},#{age},#{address},#{birthday}) delete from Person where id = #{id} update Person set name=#{name},age=#{age},birthday=#{birthday},address=#{address} where id = #{id}

测试:

public class TestMybatis { public SqlSessionFactory getfactory() throws IOException{ String filepath = "SqlMappingConfig.xml"; InputStream in = Resources.getResourceAsStream(filepath); SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(in); return sqlSessionFactory; } @Test public void testinsert() throws IOException{ SqlSessionFactory sqlSessionFactory = this.getfactory(); SqlSession sqlsession = sqlSessionFactory.openSession(); PersonMapper personMapper = sqlsession.getMapper(PersonMapper.class); Person person = new Person(); person.setName("小白"); person.setAddress("上海"); person.setAge(15); person.setBirthday("05-04"); personMapper.inserperson(person); System.out.println("id "+person.getId()); sqlsession.commit(); sqlsession.close(); } @Test public void testquerybyid() throws IOException{ SqlSessionFactory sqlSessionFactory = this.getfactory(); SqlSession sqlsession = sqlSessionFactory.openSession(); PersonMapper personMapper = sqlsession.getMapper(PersonMapper.class); Person person = personMapper.querypersonbyid(2); System.out.println(person); sqlsession.close(); } @Test public void testquerybyname() throws IOException{ SqlSessionFactory sqlSessionFactory = this.getfactory(); SqlSession sqlsession = sqlSessionFactory.openSession(); PersonMapper personMapper = sqlsession.getMapper(PersonMapper.class); List personlist = personMapper.querypersonbyname("小"); for(int i=0;i personlist = personMapper.querypersonbyvo(personVo); for(int i=0;i

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:JAVA并发基础
下一篇:Javal连接字符串为Json
相关文章

 发表评论

暂时没有评论,来抢沙发吧~