SQL构建对象介绍
查询操作
新增操作
修改操作
删除功能
核心程序
package com. itheima. bean ;
public class Student {
private Integer id;
private String name;
private Integer age;
public Student ( ) {
}
public Student ( Integer id, String name, Integer age) {
this . id = id;
this . name = name;
this . age = age;
}
public Integer getId ( ) {
return id;
}
public void setId ( Integer id) {
this . id = id;
}
public String getName ( ) {
return name;
}
public void setName ( String name) {
this . name = name;
}
public Integer getAge ( ) {
return age;
}
public void setAge ( Integer age) {
this . age = age;
}
@Override
public String toString ( ) {
return "Student{" +
"id=" + id +
", name='" + name + '\'' +
", age=" + age +
'}' ;
}
}
package com. itheima. mapper ;
import com. itheima. bean. Student ;
import com. itheima. sql. ReturnSql ;
import org. apache. ibatis. annotations. * ;
import java. util. List ;
public interface StudentMapper {
@SelectProvider ( type = ReturnSql . class , method = "getSelectAll" )
public abstract List < Student > selectAll ( ) ;
@InsertProvider ( type = ReturnSql . class , method = "getInsert" )
public abstract Integer insert ( Student student) ;
@UpdateProvider ( type = ReturnSql . class , method = "getUpdate" )
public abstract Integer update ( Student student) ;
@DeleteProvider ( type = ReturnSql . class , method = "getDelete" )
public abstract Integer delete ( Integer id) ;
}
package com. itheima. sql ;
import com. itheima. bean. Student ;
import org. apache. ibatis. jdbc. SQL;
public class ReturnSql {
public String getSelectAll ( )
{
return new SQL ( )
{
{
SELECT ( "*" ) ;
FROM ( "student" ) ;
}
} . toString ( ) ;
}
public String getInsert ( Student student)
{
return new SQL ( )
{
{
INSERT_INTO ( "student" ) ;
INTO_VALUES ( "#{id},#{name},#{age}" ) ;
}
} . toString ( ) ;
}
public String getUpdate ( Student student)
{
return new SQL ( )
{
{
UPDATE ( "student" ) ;
SET ( "name=#{name}" , "age=#{age}" ) ;
WHERE ( "id=#{id}" ) ;
}
} . toString ( ) ;
}
public String getDelete ( Integer id)
{
return new SQL ( )
{
{
DELETE_FROM ( "student" ) ;
WHERE ( "id=#{id}" ) ;
}
} . toString ( ) ;
}
}
package com. itheima. test ;
import com. itheima. bean. Student ;
import com. itheima. mapper. StudentMapper ;
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 ;
import java. util. List ;
public class Test01 {
@Test
public void selectAll ( ) throws Exception {
InputStream is = Resources . getResourceAsStream ( "MyBatisConfig.xml" ) ;
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder ( ) . build ( is) ;
SqlSession sqlSession = sqlSessionFactory. openSession ( true ) ;
StudentMapper mapper = sqlSession. getMapper ( StudentMapper . class ) ;
List < Student > list = mapper. selectAll ( ) ;
for ( Student student : list) {
System . out. println ( student) ;
}
sqlSession. close ( ) ;
is. close ( ) ;
}
@Test
public void insert ( ) throws Exception {
InputStream is = Resources . getResourceAsStream ( "MyBatisConfig.xml" ) ;
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder ( ) . build ( is) ;
SqlSession sqlSession = sqlSessionFactory. openSession ( true ) ;
StudentMapper mapper = sqlSession. getMapper ( StudentMapper . class ) ;
Student stu = new Student ( 4 , "赵六" , 26 ) ;
Integer result = mapper. insert ( stu) ;
System . out. println ( result) ;
sqlSession. close ( ) ;
is. close ( ) ;
}
@Test
public void update ( ) throws Exception {
InputStream is = Resources . getResourceAsStream ( "MyBatisConfig.xml" ) ;
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder ( ) . build ( is) ;
SqlSession sqlSession = sqlSessionFactory. openSession ( true ) ;
StudentMapper mapper = sqlSession. getMapper ( StudentMapper . class ) ;
Student stu = new Student ( 4 , "赵六" , 36 ) ;
Integer result = mapper. update ( stu) ;
System . out. println ( result) ;
sqlSession. close ( ) ;
is. close ( ) ;
}
@Test
public void delete ( ) throws Exception {
InputStream is = Resources . getResourceAsStream ( "MyBatisConfig.xml" ) ;
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder ( ) . build ( is) ;
SqlSession sqlSession = sqlSessionFactory. openSession ( true ) ;
StudentMapper mapper = sqlSession. getMapper ( StudentMapper . class ) ;
Integer result = mapper. delete ( 4 ) ;
System . out. println ( result) ;
sqlSession. close ( ) ;
is. close ( ) ;
}
}