博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
mybatis+spring配置
阅读量:6280 次
发布时间:2019-06-22

本文共 6237 字,大约阅读时间需要 20 分钟。

可参考:http://www.javacodegeeks.com/2014/02/building-java-web-application-using-mybatis-with-spring.html

自己对mybatis的spring配置不太熟,弄好了好久总算弄出来了。总结下好了!

1. 新建web maven工程。

以idea自带的spring_mvc模板开个web工程就好。

2. pom.xml配置

pom中需要新增以下jar包

  • mybatis (for MyBatis support)
  • mybatis-spring (for MyBatis-Spring integration support)
  • mysql-connector-java (for MYSQL support)
  • tomcat-jdbc (for Tomcat support MYSQL connection)
  • defind the location of source & resource 
4.1.3.RELEASE
3.3.0
1.2.3
5.1.21
7.0.30
org.mybatis
mybatis
${mybatis.version}
org.mybatis
mybatis-spring
${mybatis-spring.version}
mysql
mysql-connector-java
${mysql.version}
org.springframework
spring-jdbc
${spring.version}
org.apache.tomcat
tomcat-jdbc
${tomcat-jdbc.version}
my_test
src/main/java
src/main/resources

3. web.xml

我的实践中web.xml保持原状。

Spring MVC Application
mvc-dispatcher
org.springframework.web.servlet.DispatcherServlet
1
mvc-dispatcher
/

4. 修改mvc-dispatcher-servlet.xml

4. 各种类

因为此篇主要想讲讲如果使用在spring中进行mybatis配置,所以代码的分层不太严格。完成后代码树长这样:

4.1 controller

package com.springapp.mvc;import org.apache.ibatis.session.SqlSession;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Controller;import org.springframework.stereotype.Repository;import org.springframework.ui.ModelMap;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RequestMethod;import org.springframework.web.servlet.ModelAndView;import javax.annotation.Resource;@Controller@RequestMapping("/")public class HelloController {    @Resource    private SqlSession sqlSession;    @RequestMapping(method = RequestMethod.GET)    public ModelAndView test(ModelMap mode){
    //需结合accountMapper.xml来看。Account为namespace的值;getAccountByOwner为select的id值 Account account = sqlSession.selectOne("Account.getAccountByOwner"); ModelAndView mv = new ModelAndView(); mv.setViewName("hello"); mv.addObject("Account",account); return mv; }}

4.2 Account

package com.springapp.mvc;import java.math.BigDecimal;/** * Created by Administrator on 2015/10/25. */public class Account {    private Integer accountId;    private String account;     private String owner;    private BigDecimal balance;    public String getOwner() {        return owner;    }    public void setOwner(String owner) {        this.owner = owner;    }    public Integer getAccountId() {        return accountId;    }    public void setAccountId(Integer accountId) {        this.accountId = accountId;    }    public String getAccount() {        return account;    }    public void setAccount(String account) {        this.account = account;    }    public BigDecimal getBalance() {        return balance;    }    public void setBalance(BigDecimal balance) {        this.balance = balance;    }    public String toString(){        return "" + this.getAccountId() + "|" + this.getOwner() + "|"  +                this.getAccount() +  ";";    }}

4.3 AccountMapper

package com.springapp.mvc.mappers;import com.springapp.mvc.Account;import org.apache.ibatis.annotations.Insert;import org.apache.ibatis.annotations.Options;import org.apache.ibatis.annotations.Select;import org.springframework.stereotype.Repository;/** * Created by Administrator on 2015/11/8. */@Repositorypublic interface AccountMapper {//可以使用annotation的方式来进行数据库操作//    @Insert("INSERT INTO accounts(owner,account,type,balance) VALUES"//            + "(#{owner},#{account},#{type},#{balance})")//    @Options(useGeneratedKeys = true, keyProperty = "id", flushCache = true, keyColumn = "id")//    public void insertAccount(Account account);//也可使用xml配置方式来操作数据库,本例中即使用xml来操作数据库//    @Select("SELECT OWNER as owner, ACCOUNT as account, TYPE as type,"//            + "BALANCE as balance FROM accounts WHERE owner=#{owner}")    public Account getAccountByOwner();}

4.4 accountMapper.xml

4.4 hello.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>${Account}

5. 操作数据库,准备数据

create database accounts;use accounts;CREATE TABLE `accounts` (  `id` int(5) NOT NULL AUTO_INCREMENT,  `owner` char(10) NOT NULL,  `account` char(10) DEFAULT NULL,  `balance` decimal(9,2) DEFAULT NULL,  PRIMARY KEY (`id`)) ENGINE=InnoDB DEFAULT CHARSET=utf8INSERT accounts values(1,'haha', '中国', 12000);

6. 运行tomcat即可搜出刚才插入的数据了。

转载地址:http://rdiva.baihongyu.com/

你可能感兴趣的文章
计算机网络与Internet应用
查看>>
Django 文件下载功能
查看>>
走红日本 阿里云如何能够赢得海外荣耀
查看>>
磁盘空间满引起的mysql启动失败:ERROR! MySQL server PID file could not be found!
查看>>
点播转码相关常见问题及排查方式
查看>>
[arm驱动]linux设备地址映射到用户空间
查看>>
弗洛伊德算法
查看>>
【算法之美】求解两个有序数组的中位数 — leetcode 4. Median of Two Sorted Arrays
查看>>
精度 Precision
查看>>
Android——4.2 - 3G移植之路之 APN (五)
查看>>
Linux_DHCP服务搭建
查看>>
[SilverLight]DataGrid实现批量输入(like Excel)(补充)
查看>>
秋式广告杀手:广告拦截原理与杀手组织
查看>>
翻译 | 摆脱浏览器限制的JavaScript
查看>>
闲扯下午引爆乌云社区“盗窃”乌云币事件
查看>>
02@在类的头文件中尽量少引入其他头文件
查看>>
JAVA IO BIO NIO AIO
查看>>
input checkbox 复选框大小修改
查看>>
网吧维护工具
查看>>
BOOT.INI文件参数
查看>>