因为数据库中都是中文所以用了中文变量(顺带还试了试,居然真的能用中文类名和中文方法名)

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>top.moesy</groupId>
    <artifactId>MYSSM</artifactId>
    <version>1.0-SNAPSHOT</version>
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>6</source>
                    <target>6</target>
                </configuration>
            </plugin>
        </plugins>
        <resources>
            <resource>
                <directory>src/main/java</directory>
                <includes>
                    <include>**/*.xml</include>
                    <include>**/*.properties</include>
                </includes>
                <filtering>true</filtering>
            </resource>
            <resource>
                <directory>src/main/resources</directory>
                <includes>
                    <include>**/*.xml</include>
                    <include>**/*.properties</include>
                </includes>
                <filtering>true</filtering>
            </resource>
        </resources>
    </build>
    <dependencies>
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis</artifactId>
            <version>3.4.4</version>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.40</version>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.10</version>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>4.0.2.RELEASE</version>
        </dependency>
    </dependencies>


</project>

mybatis的配置文件

<?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">
<configuration>
    <environments default="development">
        <environment id="development">
            <transactionManager type="JDBC"/>
            <dataSource type="POOLED">
                <property name="driver" value="com.mysql.jdbc.Driver"/>
                <property name="url" value="jdbc:mysql://localhost:3306/memory"/>
                <property name="username" value="root"/>
                <property name="password" value="komeijikoishi"/>
            </dataSource>
        </environment>
    </environments>
    <mappers>
        <mapper resource="top/moesy/dao/mapper/UserMapper.xml"/>
    </mappers>
</configuration>

mybatis工具类

package top.moesy.Utils;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import javax.annotation.Resource;
import java.io.IOException;
import java.io.InputStream;

public class MybatisUtils {

    private static SqlSessionFactory sqlSessionFactory;

    static {
        try {
            String resource = "MyBatis-Config.xml";
            InputStream inputStream = Resources.getResourceAsStream(resource);
            sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    //这句是获取session连接
    public static SqlSession getSession(){
        return sqlSessionFactory.openSession();
    }
}

映射文件,因为返回全部变量所以接口里是一个List

<?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="top.moesy.dao.mapper.UserMapper">
    <select id="selectUser" resultType="top.moesy.model.User">
        select * from characterinfo
    </select>
</mapper>

以及实体类,咳咳,我不是故意放在最后一个的。

(夹带私货了一手,数据库里是他们几个的基础人设)

package top.moesy.model;

public class User {
    private Integer 创建序号;
    private String 出处;
    private String 姓名;
    private String 英文名;
    private Integer 年龄;
    private String 生日;
    private Integer 身高;
    private Integer 体重;
    private String 发色;
    private String 发型;
    private String 染发;
    private String 瞳色;
    private String 美瞳;
    private String 爱好;

    public Integer get创建序号() {
        return 创建序号;
    }

    public void set创建序号(Integer 创建序号) {
        this.创建序号 = 创建序号;
    }

    public String get出处() {
        return 出处;
    }

    public void set出处(String 出处) {
        this.出处 = 出处;
    }

    public String get姓名() {
        return 姓名;
    }

    public void set姓名(String 姓名) {
        this.姓名 = 姓名;
    }

    public String get英文名() {
        return 英文名;
    }

    public void set英文名(String 英文名) {
        this.英文名 = 英文名;
    }

    public Integer get年龄() {
        return 年龄;
    }

    public void set年龄(Integer 年龄) {
        this.年龄 = 年龄;
    }

    public String get生日() {
        return 生日;
    }

    public void set生日(String 生日) {
        this.生日 = 生日;
    }

    public Integer get身高() {
        return 身高;
    }

    public void set身高(Integer 身高) {
        this.身高 = 身高;
    }

    public Integer get体重() {
        return 体重;
    }

    public void set体重(Integer 体重) {
        this.体重 = 体重;
    }

    public String get发色() {
        return 发色;
    }

    public void set发色(String 发色) {
        this.发色 = 发色;
    }

    public String get发型() {
        return 发型;
    }

    public void set发型(String 发型) {
        this.发型 = 发型;
    }

    public String get染发() {
        return 染发;
    }

    public void set染发(String 染发) {
        this.染发 = 染发;
    }

    public String get瞳色() {
        return 瞳色;
    }

    public void set瞳色(String 瞳色) {
        this.瞳色 = 瞳色;
    }

    public String get美瞳() {
        return 美瞳;
    }

    public void set美瞳(String 美瞳) {
        this.美瞳 = 美瞳;
    }

    public String get爱好() {
        return 爱好;
    }

    public void set爱好(String 爱好) {
        this.爱好 = 爱好;
    }

    @Override
    public String toString() {
        return "Character_Info [创建序号=" + 创建序号 + ",出处=" + 出处 + ",姓名=" + 姓名 + ",英文名=" + 英文名 + ",年龄=" +
                年龄 + ",生日=" + 生日 + ",身高=" + 身高 + ",体重=" + 体重 + ",发色=" + 发色 +
                ",发型=" + 发型 + ",染发=" + 染发 + ",瞳色=" + 瞳色 + ",美瞳=" + 美瞳 +
                ",爱好=" + 爱好 + "]";
    }
}

备忘:用Maven的话,要在build标签加这个

<resources>
            <resource>
                <directory>src/main/java</directory>
                <includes>
                    <include>**/*.xml</include>
                    <include>**/*.properties</include>
                </includes>
                <filtering>true</filtering>
            </resource>
            <resource>
                <directory>src/main/resources</directory>
                <includes>
                    <include>**/*.xml</include>
                    <include>**/*.properties</include>
                </includes>
                <filtering>true</filtering>
            </resource>
</resources>

以后有了域名会慢慢学着把档案馆和code分成两个来做

耗子也要成长起来的嘛,大家不要介意哦~

Last modification:November 25th, 2020 at 01:59 pm