Mybatis_02_CRUD

 

One,

1.pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.cmdzz</groupId>
    <artifactId>mybatis_day02_CRUD</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>jar</packaging>

    <dependencies>
        <!--mybatis-->
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis</artifactId>
            <version>3.4.5</version>
        </dependency>

        <!--mysql连接-->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.6</version>
        </dependency>

        <!--log4j-->
        <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>1.2.12</version>
        </dependency>

        <!--junit-->
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.10</version>
        </dependency>


    </dependencies>

</project>

 

2.SqlMapConfgi.xml

<?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">

<-! The mybatis main configuration file -> 
< the Configuration > 
    <-! Configuration environment
        default = "mysql" was written just below the environment id to be consistent
    -> 
    < Environments default = "mysql" > 
        <-! Mysql configuration environment -> 
        < Environment the above mentioned id = "mysql" > 
            <-! Type Configuration Services -> 
            < transactionManager of the type = "JDBC" > </ the transactionManager > 
            <-! configuration data source (connection pool) -> 
            < the dataSource type = "the POOLED" > 
                <-! . 4 basic configuration database connection information -> 
                < Property name = "Driver" value = "COM .mysql.jdbc.Driver "/>
                <property name="url" value="jdbc:mysql://localhost:3306/mybatis57plage"/>
                <property name="username" value="root"/>
                <property name="password" value="123456"/>
            </dataSource>
        </environment>
    </environments>

    <! - Specifies the location mapping configuration file, a mapping configuration file refers to each separate profiles dao -> 
    < by mappers > 
        < Mapper Resource = "COM / cmdzz / dao / IUserDao.xml" /> 
    </ by mappers >

    <! - Specifies the location mapping configuration file, a mapping configuration file refers to each separate profiles dao
       If the annotation is configured, it should here be specified class attribute annotated dao fully qualified class name
   -->
  <!--  <mappers>
        <mapper class="com.cmdzz.dao.IUserDao"/>
    </mappers>-->
</configuration>

 

3.IUserDao.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 = "com.cmdzz.dao.IUserDao" > 
    <-! Configured to query all
        id = "findAll" is the name of the method to be consistent UserDao
    -->
    <select id="findAll" resultType="com.cmdzz.domain.User">
        select * from user
    </select>

    <!--保存用户-->
    <insert id="saveUser" parameterType="com.cmdzz.domain.User">
        insert into user(username,address,sex,birthday)value (#{userName},#{address},#{sex},#{birthday})

    </insert>

    <!--更新用户-->
    <update id="updateUser" parameterType="com.cmdzz.domain.User">
        update user set username=#{userName},address=#{address},sex=#{sex},birthday=#{birthday}
        where
        id=#{id}

    </update>

    <! - delete the user interface since only one parameter passed in, the placeholder # {} which can easily write -> 
    < Delete ID = "deleteUser" the parameterType = "java.lang.Integer" > 
        Delete User from WHERE = {# ID UID }
     </ Delete > 
</ Mapper >

 

Guess you like

Origin www.cnblogs.com/cmdzhizhu/p/11091697.html