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&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>
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; @Service public 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、測試插入數據。