wKioL1VAasqyfKaSAACJ0YFtjVM477.jpg

数据模型对象mybatis.pojo.MovieFilm.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
package  mybatis.pojo;
 
/**
  * Created by Administrator on 2015/4/29.
  */
public  class  MovieFilm {
     private  Long id;
     private  String filmNo;
     private  String filmName;
     private  String language;
     private  String filmDesc;
 
     public  Long getId() {
         return  id;
     }
 
     public  void  setId(Long id) {
         this .id = id;
     }
 
     public  String getFilmNo() {
         return  filmNo;
     }
 
     public  void  setFilmNo(String filmNo) {
         this .filmNo = filmNo;
     }
 
     public  String getFilmName() {
         return  filmName;
     }
 
     public  void  setFilmName(String filmName) {
         this .filmName = filmName;
     }
 
     public  String getLanguage() {
         return  language;
     }
 
     public  void  setLanguage(String language) {
         this .language = language;
     }
 
     public  String getFilmDesc() {
         return  filmDesc;
     }
 
     public  void  setFilmDesc(String filmDesc) {
         this .filmDesc = filmDesc;
     }
 
     @Override
     public  String toString() {
         return  "MovieFilm{"  +
                 "id="  + id +
                 ", filmNo='"  + filmNo + '\ ''  +
                 ", filmName='"  + filmName + '\ ''  +
                 ", language='"  + language + '\ ''  +
                 ", filmDesc='"  + filmDesc + '\ ''  +
                 '}' ;
     }
}


MyBatis配置文件mybatis-config.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
<? 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 >
     < settings >
         <!-- changes from the defaults for testing -->
         < setting  name = "cacheEnabled"  value = "false"  />
         < setting  name = "useGeneratedKeys"  value = "true"  />
         < setting  name = "defaultExecutorType"  value = "REUSE"  />
     </ settings >
 
     < typeAliases >
         < typeAlias  alias = "MovieFilm"  type = "mybatis.pojo.MovieFilm" />
     </ typeAliases >
 
     < environments  default = "development" >
         < environment  id = "development" >
             < transactionManager  type = "jdbc" />
             < dataSource  type = "POOLED" >
                 < property  name = "driver"  value = "com.mysql.jdbc.Driver" />
                 < property  name = "url"  value = "jdbc:mysql://192.168.121.219:3306/test" />
                 < property  name = "username"  value = "business_dev" />
                 < property  name = "password"  value = "businessdev0722" />
             </ dataSource >
         </ environment >
     </ environments >
 
     < mappers >
         < mapper  resource = "mybatis/config/mappers/MovieFilmMapper.xml"  />
     </ mappers >
 
</ configuration >


Mybatis Mapper配置文件MovieFilmMapper.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
<? 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 = "movie_film" >
 
     < insert  id = "insertMovieFilm"  parameterType = "MovieFilm" >
         insert into MOVIE_FILM(ID,FILM_NO,FILM_NAME,LANGUAGE ,FILM_DESC) values(#{id},#{filmNo},#{filmName},#{language},#{filmDesc})
     </ insert >
 
     < insert  id = "updateMovieFilm"  parameterType = "MovieFilm" >
         update MOVIE_FILM set
             FILM_NO = #{filmNo},
             FILM_NAME=#{filmName},
             LANGUAGE=#{language},
             FILM_DESC=#{filmDesc}
         where id=#{id}
     </ insert >
 
     < delete  id = "deleteMovieFilm"  parameterType = "java.lang.Long" >
         delete from MOVIE_FILM where id=#{id}
     </ delete >
 
     < select  id = "getAllMovieFilm"  resultType = "MovieFilm" >
         select * from MOVIE_FILM
     </ select >
</ mapper >


测试类Main.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
package  mybatis;
 
import  mybatis.pojo.MovieFilm;
import  org.apache.ibatis.io.Resources;
import  org.apache.ibatis.session.RowBounds;
import  org.apache.ibatis.session.SqlSession;
import  org.apache.ibatis.session.SqlSessionFactory;
import  org.apache.ibatis.session.SqlSessionFactoryBuilder;
 
import  java.io.IOException;
import  java.io.Reader;
import  java.util.List;
import  java.util.Random;
 
public  class  Main {
     private  static  SqlSession openSqlSession(){
         SqlSessionFactory sqlSessionFactory =  null ;
 
         String resource =  "mybatis/config/mybatis-config.xml" ;
         Reader reader =  null ;
         try  {
             reader = Resources.getResourceAsReader(resource);
         catch  (IOException e) {
             throw  new  RuntimeException(e);
 
         }
         sqlSessionFactory =  new  SqlSessionFactoryBuilder().build(reader);
 
         SqlSession SqlSession= sqlSessionFactory.openSession();
 
         return  SqlSession;
     }
 
 
     private  static  void  select(){
         SqlSession sqlSession = openSqlSession();
         RowBounds rowBounds =  new  RowBounds( 0 , 2 );
         List<MovieFilm> list = sqlSession.selectList( "movie_film.getAllMovieFilm" , null ,rowBounds);
         sqlSession.close();
         System.out.println(list.size());
         System.out.println(list);
     }
 
     private  static  void  insert(){
         SqlSession sqlSession = openSqlSession();
         MovieFilm movieFilm =  new  MovieFilm();
         movieFilm.setId( new  Random().nextLong());
         sqlSession.insert( "movie_film.insertMovieFilm" , movieFilm);
         sqlSession.commit();
         sqlSession.close();
     }
 
     private  static  void  update(){
         SqlSession sqlSession = openSqlSession();
         MovieFilm movieFilm =  new  MovieFilm();
         movieFilm.setId(1l);
         movieFilm.setFilmNo( "2" );
         movieFilm.setLanguage( "" );
         sqlSession.update( "movie_film.updateMovieFilm" , movieFilm);
         sqlSession.commit();
         sqlSession.close();
     }
 
     private  static  void  delete(){
         SqlSession sqlSession = openSqlSession();
         sqlSession.delete( "movie_film.deleteMovieFilm" , 3 );
         sqlSession.commit();
         sqlSession.close();
     }
 
 
     public  static  void  main(String[] args) {
         select();
 
         insert();
 
         update();
 
         delete();
     }
 
 
}


pom.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
<!--=========================== mysql =========================== -->
< dependency >
     < groupId >mysql</ groupId >
     < artifactId >mysql-connector-java</ artifactId >
     < version >5.1.21</ version >
</ dependency >
 
<!--=========================== mybatis =========================== -->
< dependency >
     < groupId >org.mybatis</ groupId >
     < artifactId >mybatis</ artifactId >
     < version >3.1.1</ version >
</ dependency >