1.新建maven項目java
2.項目基礎配置mysql
找到maven工程所在目錄下org.eclipse.wst.common.project.facet.core.xml此文件
web修改爲以下內容sql
maven工程下的web.xml文件替換爲數據庫
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" version="3.1" metadata-complete="true"> </web-app>
pom.xml文件中修改build節點,添加以下內容apache
<plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.1</version> <configuration> <source>1.7</source> <target>1.7</target> </configuration> </plugin>3.在pom.xml追加以下內容mybatis
<dependency> <groupId>org.mybatis.generator</groupId> <artifactId>mybatis-generator-core</artifactId> <version>1.3.2</version> </dependency>
<plugin> <groupId>org.mybatis.generator</groupId> <artifactId>mybatis-generator-maven-plugin</artifactId> <version>1.3.2</version> <configuration> <!-- mybatis-generator的配置文件,根據狀況調整位置 --> <configurationFile>src/main/resources/generatorConfig.xml</configurationFile> <verbose>true</verbose> <overwrite>true</overwrite> </configuration> <dependencies> <!-- 數據庫驅動 --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.34</version> </dependency> </dependencies> <executions> <execution> <id>Generate MyBatis Artifacts</id> <goals> <goal>generate</goal> </goals> </execution> </executions> </plugin>
4.在src/main/resources文件下建立generatorConfig.xml,內容以下app
<?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"> <!-- 配置Run As Maven build : Goals 參數 : mybatis-generator:generate -Dmybatis.generator.overwrite=true --> <!-- 配置 tableName,使用 Run As Maven build 生成 dao model 層 --> <generatorConfiguration> <!-- 配置文件路徑 <properties url="${mybatis.generator.generatorConfig.properties}"/>--> <context id="DB2Tables" targetRuntime="MyBatis3"> <!--關閉註釋 --> <commentGenerator> <property name="suppressAllComments" value="true"/> </commentGenerator> <!--數據庫鏈接信息 --> <jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8" userId="root" password="123456"> </jdbcConnection> <!--生成的model 包路徑 --> <javaModelGenerator targetPackage="com.lymn.it.model" targetProject="src/main/java"> <property name="enableSubPackages" value="ture"/> <property name="trimStrings" value="true"/> </javaModelGenerator> <!--生成xml mapper文件 路徑 --> <sqlMapGenerator targetPackage="com.lymn.it.mapper" targetProject="src/main/java"> <property name="enableSubPackages" value="ture"/> </sqlMapGenerator> <!-- 生成的Dao接口 的包路徑 --> <javaClientGenerator type="XMLMAPPER" targetPackage="com.lymn.it.mapper" targetProject="src/main/java"> <property name="enableSubPackages" value="ture"/> </javaClientGenerator> <!--對應數據庫表名,多個表,請複製指定 --> <table tableName="user" domainObjectName="User" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"> </table> </context> </generatorConfiguration>
5.右鍵項目--》run As--》Maven build--》輸入mybatis-generator:generate,便可建立成功。框架
6.生成代碼以下dom
package com.lymn.it.mapper; import com.lymn.it.model.User; public interface UserMapper { int deleteByPrimaryKey(Integer userid); int insert(User record); int insertSelective(User record); User selectByPrimaryKey(Integer userid); int updateByPrimaryKeySelective(User record); int updateByPrimaryKey(User record); }
<?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.lymn.it.mapper.UserMapper"> <resultMap id="BaseResultMap" type="com.lymn.it.model.User"> <id column="userid" jdbcType="INTEGER" property="userid" /> <result column="username" jdbcType="VARCHAR" property="username" /> <result column="password" jdbcType="VARCHAR" property="password" /> <result column="email" jdbcType="VARCHAR" property="email" /> <result column="phone" jdbcType="VARCHAR" property="phone" /> <result column="status" jdbcType="VARCHAR" property="status" /> <result column="code" jdbcType="VARCHAR" property="code" /> </resultMap> <sql id="Base_Column_List"> userid, username, password, email, phone, status, code </sql> <select id="selectByPrimaryKey" parameterType="java.lang.Integer" resultMap="BaseResultMap"> select <include refid="Base_Column_List" /> from user where userid = #{userid,jdbcType=INTEGER} </select> <delete id="deleteByPrimaryKey" parameterType="java.lang.Integer"> delete from user where userid = #{userid,jdbcType=INTEGER} </delete> <insert id="insert" parameterType="com.lymn.it.model.User"> insert into user (userid, username, password, email, phone, status, code) values (#{userid,jdbcType=INTEGER}, #{username,jdbcType=VARCHAR}, #{password,jdbcType=VARCHAR}, #{email,jdbcType=VARCHAR}, #{phone,jdbcType=VARCHAR}, #{status,jdbcType=VARCHAR}, #{code,jdbcType=VARCHAR}) </insert> <insert id="insertSelective" parameterType="com.lymn.it.model.User"> insert into user <trim prefix="(" suffix=")" suffixOverrides=","> <if test="userid != null"> userid, </if> <if test="username != null"> username, </if> <if test="password != null"> password, </if> <if test="email != null"> email, </if> <if test="phone != null"> phone, </if> <if test="status != null"> status, </if> <if test="code != null"> code, </if> </trim> <trim prefix="values (" suffix=")" suffixOverrides=","> <if test="userid != null"> #{userid,jdbcType=INTEGER}, </if> <if test="username != null"> #{username,jdbcType=VARCHAR}, </if> <if test="password != null"> #{password,jdbcType=VARCHAR}, </if> <if test="email != null"> #{email,jdbcType=VARCHAR}, </if> <if test="phone != null"> #{phone,jdbcType=VARCHAR}, </if> <if test="status != null"> #{status,jdbcType=VARCHAR}, </if> <if test="code != null"> #{code,jdbcType=VARCHAR}, </if> </trim> </insert> <update id="updateByPrimaryKeySelective" parameterType="com.lymn.it.model.User"> update user <set> <if test="username != null"> username = #{username,jdbcType=VARCHAR}, </if> <if test="password != null"> password = #{password,jdbcType=VARCHAR}, </if> <if test="email != null"> email = #{email,jdbcType=VARCHAR}, </if> <if test="phone != null"> phone = #{phone,jdbcType=VARCHAR}, </if> <if test="status != null"> status = #{status,jdbcType=VARCHAR}, </if> <if test="code != null"> code = #{code,jdbcType=VARCHAR}, </if> </set> where userid = #{userid,jdbcType=INTEGER} </update> <update id="updateByPrimaryKey" parameterType="com.lymn.it.model.User"> update user set username = #{username,jdbcType=VARCHAR}, password = #{password,jdbcType=VARCHAR}, email = #{email,jdbcType=VARCHAR}, phone = #{phone,jdbcType=VARCHAR}, status = #{status,jdbcType=VARCHAR}, code = #{code,jdbcType=VARCHAR} where userid = #{userid,jdbcType=INTEGER} </update> </mapper>
package com.lymn.it.model; public class User { private Integer userid; private String username; private String password; private String email; private String phone; private String status; private String code; public Integer getUserid() { return userid; } public void setUserid(Integer userid) { this.userid = userid; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username == null ? null : username.trim(); } public String getPassword() { return password; } public void setPassword(String password) { this.password = password == null ? null : password.trim(); } public String getEmail() { return email; } public void setEmail(String email) { this.email = email == null ? null : email.trim(); } public String getPhone() { return phone; } public void setPhone(String phone) { this.phone = phone == null ? null : phone.trim(); } public String getStatus() { return status; } public void setStatus(String status) { this.status = status == null ? null : status.trim(); } public String getCode() { return code; } public void setCode(String code) { this.code = code == null ? null : code.trim(); } }
8.參照SSM框架搭建之傳統方式(一)博客中的service、controller層進行測試