JavaWeb——MyBatis入门程序

来源:互联网 发布:淘宝网电脑版登录 怎么 编辑:程序博客网 时间:2024/03/28 20:06

一、引言


一般MyBatis与springMVC常常一起使用,而且与hibernate相比有着天然的优势,继续推进。




     MyBatis应用程序根据XML配置文件创建SqlSessionFactory,SqlSessionFactory在根据配置,配置来源于两个地方,一处是配置文件,一处是Java代码的注解,获取一个SqlSession。SqlSession包含了执行sql所需要的所有方法,可以通过SqlSession实例直接运行映射的sql语句,完成对数据的增删改查和事务提交等,用完之后关闭SqlSession。


二、上代码


文件结构如下:


1、eclipse与tomcat配置不用说,不会的看前面的博文;


2、下载所需的点击打开链接mybatis,还需要mysql的jdbc,添加到lib文件夹


3、添加上述圈红文件,都是重点!!!


4、配置xml文件mapConfig

包括数据库链接和mapper文件

<?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>          <!-- 对事务的管理和连接池的配置 -->      <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://localhost:3306/yellowbike" />                <property name="username" value="root" />                  <property name="password" value="1234" />            </dataSource>          </environment>      </environments>       <mappers>         <mapper resource="map/bike.xml"/>     </mappers></configuration>


5、配置mapper文件

这个配置的是根据id查询数据

<?xml version="1.0" encoding="UTF-8" ?><!DOCTYPE mapperPUBLIC "-//mybatis.org//DTD Mapper 3.0//EN""http://mybatis.org/dtd/mybatis-3-mapper.dtd"><mapper namespace="map.bike"><select id="findBike" parameterType="java.lang.String" resultType="com.xcy.po.Bike">select * from T_BIKE where F_CODE = #{id}</select><insert id="addBike" parameterType="com.xcy.po.Bike">insert into T_BIKE (F_CODE,F_PW)values(#{F_CODE},#{F_PW})</insert></mapper>


6、添加mapper对应的po

package com.xcy.po;public class Bike {private int F_ID;private String F_CODE;private String F_PW;public int getF_ID() {return F_ID;}public void setF_ID(int f_ID) {F_ID = f_ID;}public String getF_CODD() {return F_CODE;}public void setF_CODE(String f_CODE) {F_CODE = f_CODE;}public String getF_PW() {return F_PW;}public void setF_PW(String f_PW) {F_PW = f_PW;}@Overridepublic String toString() {return "Bike [F_ID=" + F_ID + ", F_CODD=" + F_CODE + ", F_PW=" + F_PW + "]";}}


7、创建session调用配置好的接口

读取配置文件mapConfig,获得映射关系,然后创建session调用CRUD等方法。

@RequestMapping(value="/getBike",method= RequestMethod.POST)public ModelAndView getBike(HttpServletRequest request, HttpServletResponse response) {// TODO Auto-generated method stubBike bike=new Bike();try {InputStream inputStream;inputStream = Resources.getResourceAsStream("mapConfig.xml");SqlSessionFactoryBuilder sqlSessionFactoryBuilder = new SqlSessionFactoryBuilder();SqlSessionFactory sqlSessionFactory = sqlSessionFactoryBuilder.build(inputStream);SqlSession sqlSession = sqlSessionFactory.openSession();String code= request.getParameter("code");    bike= sqlSession.selectOne("map.bike.findBike", code);System.out.println(bike);sqlSession.close();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}ModelAndView modelAndView = new ModelAndView();modelAndView.addObject("bike", bike);modelAndView.setViewName("bikeselect");return modelAndView;}


三、总结


  • MyBatis基本介绍

  • MyBatis基本环境配置

  • MyBatis入门程序

0 0
原创粉丝点击