数据模型对象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
<?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
<?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
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
<!--=========================== 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>