mybatis学习:一
来源:互联网 发布:js options的用法 编辑:程序博客网 时间:2024/06/05 01:51
导入需要使用到的jar包
1.创建表
CREATE TABLE `book` ( `bookId` int(11) NOT NULL auto_increment, `bookName` varchar(20) default NULL, `bookAuthor` varchar(200) default NULL, `bookPrice` decimal(9,2) default NULL, `bookInfo` longtext, PRIMARY KEY (`bookId`))
2.创建实体bean
public class Book implements Serializable{private int bookId;private String bookName;private String bookAuthor;private double bookPrice;private String bookInfo;public int getBookId() {return bookId;}public void setBookId(int bookId) {this.bookId = bookId;}public String getBookName() {return bookName;}public void setBookName(String bookName) {this.bookName = bookName;}public String getBookAuthor() {return bookAuthor;}public void setBookAuthor(String bookAuthor) {this.bookAuthor = bookAuthor;}public double getBookPrice() {return bookPrice;}public void setBookPrice(double bookPrice) {this.bookPrice = bookPrice;}public String getBookInfo() {return bookInfo;}public void setBookInfo(String bookInfo) {this.bookInfo = bookInfo;}@Overridepublic String toString() {return "Book [bookId=" + bookId + ", bookName=" + bookName + ", bookAuthor=" + bookAuthor + ", bookPrice="+ bookPrice + ", bookInfo=" + bookInfo + "]\r\n";}}
3.在src下创建配置文件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> <typeAliases> <typeAlias type="com.han.mybatis.vo.Book" alias="Book"/> </typeAliases> <!-- 配置环境(支持多个环境) --> <environments default="development"> <!-- 其中一个环境 --> <environment id="development"> <!-- 事务管理(JDBC事务) --> <transactionManager type="JDBC"></transactionManager> <!-- 数据源 --> <dataSource type="UNPOOLED"> <property name="driver" value="com.mysql.jdbc.Driver"/> <property name="url" value="jdbc:mysql://localhost:3306/mybatis"/> <property name="username" value="root"/> <property name="password" value="********"/> </dataSource> </environment> </environments> <mappers> <mapper resource="com/han/mybatis/vo/BookMapper.xml"/> </mappers></configuration>
4.创建方法映射文件BookMapper.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="com.han"> <select id="loadAll" resultType="Book"> select * from Book </select> <insert id="save" parameterType="Book"> insert into Book(bookName,bookAuthor,BookPrice,bookInfo) values(#{bookName},#{bookAuthor},#{bookPrice},#{bookInfo}) </insert> <delete id="delete" parameterType="int"> delete from Book where bookId=#{id} </delete> <update id="update" parameterType="Book"> update book set bookName=#{bookName},bookAuthor=#{bookAuthor}, bookPrice=#{bookPrice},bookInfo=#{bookInfo} where bookId=#{bookId} </update> <select id="loadById" resultType="Book" parameterType="int"> select * from book where bookId=#{id} </select></mapper>
5.在主函数中测试
public class Main { public static void main(String[] args) { // TODO Auto-generated method stub InputStream inputStream = null; SqlSession session = null; try { inputStream = Resources.getResourceAsStream("mybatis-config.xml"); SqlSessionFactory sessionFactory=new SqlSessionFactoryBuilder().build(inputStream); session=sessionFactory.openSession(); List<Book> books=session.selectList("com.han.loadAll"); System.out.println(books); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); }finally { session.close(); try { inputStream.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }}
阅读全文
1 0
- mybatis学习一
- Mybatis学习日记(一)
- mybatis学习(一)
- Mybatis学习(一)
- mybatis学习(一)
- MyBatis学习(一)
- MyBatis学习(一)
- MyBatis学习总结一
- MyBatis 学习笔记一
- MyBatis学习一
- MyBatis学习笔记一
- Mybatis学习总结一
- 学习Mybatis(一)
- MyBatis学习一
- Mybatis学习一
- Mybatis学习记录一
- mybatis学习笔记一
- MyBatis学习总结(一)
- 201705031-leetcode-155-Min Stack
- 图片加载开源库 Glide 用法详解
- 后台系统开发
- 如何按国家标准分幅下载卫星地图
- 百度地图定位错误UnsatisfiedLinkError; thrown while initializing Lcom/baidu/platform/comapi/a的解决办法
- mybatis学习:一
- Ubuntu常用软件安装
- 第一课
- VerQueryValue函数从指定的版本信息资源中检索指定的版本信息。
- 为Docker容器指定自定义网段的固定IP/静态IP地址
- jQuery选择器
- 保险与护身符
- Thinkphp输出多个,号隔开的数据
- 记录自已学习之面试题2