数据模型对象mybatis.pojo.MovieFilm.java
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
Mybatis Mapper配置文件MovieFilmMapper.xml
insert into MOVIE_FILM(ID,FILM_NO,FILM_NAME,LANGUAGE ,FILM_DESC) values(#{id},#{filmNo},#{filmName},#{language},#{filmDesc}) update MOVIE_FILM set FILM_NO = #{filmNo}, FILM_NAME=#{filmName}, LANGUAGE=#{language}, FILM_DESC=#{filmDesc} where id=#{id} delete from MOVIE_FILM where id=#{id}
测试类Main.java
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); Listlist = 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
mysql mysql-connector-java 5.1.21 org.mybatis mybatis 3.1.1