ssm架構整合筆記-3-mybatis的逆向工程與測試專案,ssm-3-mybatis

來源:互聯網
上載者:User

ssm架構整合筆記-3-mybatis的逆向工程與測試專案,ssm-3-mybatis
1、mybatis代碼產生器(外掛程式) 1.1 安裝外掛程式

資源準備:
mybatis_generator_1.3.1.zip
generatorConfig.xml

1.1.1 關閉MyEclipse,找到MyEclipse安裝後的目錄,建立檔案夾soft/mybatis,如:

<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE generatorConfiguration PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN" "http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd"><generatorConfiguration> <!-- <properties resource="conn.properties" /> --> <!-- 為產生器指定資料庫連接的驅動 --> <classPathEntry location="D:\Genuitec\MyEclipse 10\soft\mybatis\mysql-connector-java-5.1.26-bin.jar" /> <!-- 指定運行環境是mybatis3的版本 --> <context id="testTables" targetRuntime="MyBatis3"> <commentGenerator> <!-- 是否取消注釋 --> <property name="suppressAllComments" value="true" /> <!-- 是否產生注釋代時間戳記 --> <property name="suppressDate" value="true" /> </commentGenerator> <!-- jdbc 串連資訊 --> <jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://localhost:3306/scm?useUnicode=true&amp;characterEncoding=UTF-8" userId="root" password="root"> </jdbcConnection> <!-- targetPackage指定模型在產生在哪個包 ,targetProject指定項目的src, --> <javaModelGenerator targetPackage="cn.itcast.scm.entity" targetProject="scm/src"> <!-- 去除欄位前後空格 ,與javabean的set/get方法有關,不過一般我們現在寫的set/get不存在這種情況,預設false就行 --> <property name="trimStrings" value="false" /> </javaModelGenerator> <!-- 配置SQL對應檔產生資訊 --> <sqlMapGenerator targetPackage="cn.itcast.scm.dao" targetProject="scm/src" /> <!--配置dao介面產生資訊 --> <javaClientGenerator type="XMLMAPPER" targetPackage="cn.itcast.scm.dao" targetProject="scm/src" /> <!-- 需要產生的資料庫表 提醒:如果某表產生過一次了,下次需要注釋掉(或刪掉), 因為該產生器沒有那麼智能,會重複產生屬性和set/get --> <table tableName="account" domainObjectName="Account" /> <table tableName="supplier" domainObjectName="Supplier" /> </context></generatorConfiguration>2、準備資料庫和表

drop database if exists scm;create database scm default character set utf8;use scm;drop table if exists supplier;/*============================*//* Table: 管理員表結構           *//*============================*/create table account(   /* 管理員編號,自動成長 */   acc_id                  int not null auto_increment,   /* 管理員登入名稱  */   acc_login               varchar(20),   /* 管理員姓名  */   acc_name                varchar(20),   /* 管理員密碼 */   acc_pass                varchar(20),   /* 設定編號為主鍵 */   primary key (acc_id));/*==============================================================*//* Table: 供應商表                                            *//*==============================================================*/create table supplier(   sup_id               int not null auto_increment,   sup_name             varchar(20),   sup_linkman          varchar(20),   sup_phone            varchar(11),   sup_address          varchar(100),   sup_remark           varchar(200),   primary key (sup_id));/* 添加供應商資料, */insert into account (acc_login,acc_pass) values ('admin','admin');insert into account (acc_login,acc_pass) values ('admin2','admin2');/* 添加供應商資料, */insert into supplier (sup_name,sup_linkman,sup_phone,sup_address,sup_remark) values ('蘋果供應商','小張','12388888887','廣州花都','普通供應商');insert into supplier (sup_name,sup_linkman,sup_phone,sup_address,sup_remark) values ('三星供應商','小王','12388888888','廣州增城','普通供應商');insert into supplier (sup_name,sup_linkman,sup_phone,sup_address,sup_remark) values ('華為供應商','小王','12388888889','廣東深圳','一級供應商');select * from account;select * from supplier;

 

3、開始產生代碼

3.1 產生後的項目代碼,Entity部分包含一些我們沒用的資訊,可以刪除掉,只留下我們需要的。

產生成功,產生後:package cn.itcast.scm.dao;import cn.itcast.scm.entity.Account;public interface AccountMapper { int insertAccount(Account record);}

4.1.2修改AccountMapper.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="cn.itcast.scm.dao.AccountMapper" >  <resultMap id="accountResultMap" type="cn.itcast.scm.entity.Account" >    <id column="acc_id" property="accId" jdbcType="INTEGER" />    <result column="acc_login" property="accLogin" jdbcType="VARCHAR" />    <result column="acc_name" property="accName" jdbcType="VARCHAR" />    <result column="acc_pass" property="accPass" jdbcType="VARCHAR" />  </resultMap>    <insert id="insert" parameterType="cn.itcast.scm.entity.Account" >    insert into account (acc_name) values (#{accName})  </insert>   </mapper>

4.1.3 修改產生的實體類Account

package cn.itcast.scm.entity;import java.io.Serializable;public class Account implements Serializable {    private static final long serialVersionUID = -649693483437792411L;    private Integer accId;    private String accLogin;    private String accName;    private String accPass;    //此處省略了setter/getter方法,自行補上    ...        @Override    public String toString() {        return "Account [accId=" + accId + ", accLogin=" + accLogin                + ", accName=" + accName + ", accPass=" + accPass + "]";    }    }

 

4.2 添加Service介面和實作類別,添加控制器類。

4.2.1 AccountService.java

package cn.itcast.scm.service;import cn.itcast.scm.entity.Account;public interface AccountService {    public int insertAccount(Account account) throws Exception;}

4.2.2 AccountServiceImpl.java

package cn.itcast.scm.service.impl;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Service;import cn.itcast.scm.dao.AccountMapper;import cn.itcast.scm.entity.Account;import cn.itcast.scm.service.AccountService;@Servicepublic class AccountServiceImpl implements AccountService{    @Autowired    AccountMapper accountMapper;    @Override    public int insertAccount(Account account) throws Exception {        return accountMapper.insertAccount(account);    }}
4.2.3 AccountAction.java
package cn.itcast.scm.action;import javax.annotation.Resource;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.RequestMapping;import cn.itcast.scm.entity.Account;import cn.itcast.scm.service.AccountService;@Controller@RequestMapping(value = "/account")public class DeptAction {    @Resource    private AccountService accountService;    @RequestMapping(value = "/insert")    public String insert(Account account) {        try {            System.out.println("---action.account:" + account);            accountService.insertAccount(account);        } catch (Exception e) {            e.printStackTrace();        }        return "forward:/jsp/main.jsp";    }}

4.2.4、修改index.jsp,用以測試插入記錄

index.jsp

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%><html><head></head><body>    <form action="account/insert.action" method="post">        管理員名稱:<input type="text" name="accName"><br>         <input type="submit" value="ok">    </form></body></html>

 

5、測試插入資料。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.