SSM——Mybatis测试

SSM——Mybatis测试

1.映射文件

<?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>
    <!-- 配置properties-->
    <properties resource="db.properties"></properties>

    <!--使用typeAliases配置别名,它只能配置domain中类的别名 -->
    <typeAliases>
        <package name="com.w.domain"></package>
    </typeAliases>

    <!--配置环境-->
    <environments default="mysql">
        <!-- 配置mysql的环境-->
        <environment id="mysql">
            <!-- 配置事务 -->
            <transactionManager type="JDBC"></transactionManager>

            <!--配置连接池-->
            <dataSource type="POOLED">
                <property name="driver" value="${driver}"></property>
                <property name="url" value="${url}"></property>
                <property name="username" value="${username}"></property>
                <property name="password" value="${password}"></property>
            </dataSource>
        </environment>
    </environments>
    <!-- 配置映射文件的位置 -->
    <mappers>
        <package name="com.w.dao"></package>
    </mappers>
</configuration>

2.测试函数

    @Test
    public void run() throws IOException {
        InputStream input = Resources.getResourceAsStream("user.xml");
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(input);
        SqlSession session = sqlSessionFactory.openSession();
        UserDao userDao = session.getMapper(UserDao.class);
        List<User> lists = userDao.findAll();
        for(User a: lists){
            System.out.println(a.toString());;
        }
    }

3.数据库连接配置文件

driver=com.mysql.cj.jdbc.Driver

url=jdbc:mysql://localhost:3306/test1?serverTimezone=PRC

username=root

password=123456

#druid dataSource

initialSize=10

minIdle=10

maxActive=50

maxWait=60000

timeBetweenEvictionRunsMillis=30000

minEvictableIdleTimeMillis=150000

validationQuery=select 1 from dual

validationQueryTimeout=3600

testWhileIdle=true

testOnBorrow=false

testOnReturn=false

removeAbandoned=true

removeAbandonedTimeout=1800000

logAbandoned=true

poolPreparedStatements=true

maxPoolPreparedStatementPerConnectionSize=20

filters=wall,stat

4.dao层配置

@Repository
public interface UserDao {
//    查询用户是否存在
    @Select("select * form user where username = #{user.username} and password = #{user.password}")
    User IsUserExit(User user);

    @Select("select * from user where username = #{name}")
    User findUserByName(String name);
//    查询指定用户
    @Select("select * from user where username = #{user.username} and password = #{user.password}")
    User findUser(User user);

//    查询所有用户
    @Select("select * from user")
    List<User> findAll();
}

5.实体类配置

public class User {

    private int userID;
    private String username;
    private String password;
    private String telephone;
    private String email;
    private String status;

    public int getUserID() {
        return userID;
    }

    public void setUserID(int userID) {
        this.userID = userID;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }
    public String getTelephone() {
        return telephone;
    }

    public void setTelephone(String telephone) {
        this.telephone = telephone;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值