数据库连接,并测试增删改查

2019-03-12  本文已影响0人  Monica_b73c

1.建立基本的模块框架,导入需要使用的配置文件 image.png

jdbc.properties文件的配置

jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/db_spring?useUnicode=true&characterEncoding=utf-8
jdbc.username=root //这里填写你自己MySQL的账户名
jdbc.password=root //这里填写你自己的MySQL密码

pom.xml文件的配置

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<spring.version>5.1.5.RELEASE</spring.version>
<aspectj.version>1.9.2</aspectj.version>
<junit.version>4.12</junit.version>
<log4j.version>1.2.17</log4j.version>
<slf4j.version>1.7.12</slf4j.version>
<mysql.version>5.1.47</mysql.version>
<mybatis.version>3.5.0</mybatis.version>
<mybatis-spring.version>2.0.0</mybatis-spring.version>
<tk-mybatis.version>4.1.5</tk-mybatis.version>
<druid.version>1.1.14</druid.version>
<lombok.version>1.18.6</lombok.version>
</properties>

<dependencies>
    <!--Spring相关依赖-->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
        <version>${spring.version}</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-tx</artifactId>
        <version>${spring.version}</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-jdbc</artifactId>
        <version>${spring.version}</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-test</artifactId>
        <version>${spring.version}</version>
    </dependency>

    <!--mysql依赖-->
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>${mysql.version}</version>
    </dependency>

    <!--mybatis依赖-->
    <dependency>
        <groupId>org.mybatis</groupId>
        <artifactId>mybatis</artifactId>
        <version>${mybatis.version}</version>
    </dependency>

    <!--mybatis-spring依赖-->
    <dependency>
        <groupId>org.mybatis</groupId>
        <artifactId>mybatis-spring</artifactId>
        <version>${mybatis-spring.version}</version>
    </dependency>

    <!--通用mapper依赖-->
    <dependency>
        <groupId>tk.mybatis</groupId>
        <artifactId>mapper</artifactId>
        <version>${tk-mybatis.version}</version>
    </dependency>

    <!--druid连接池依赖-->
    <dependency>
        <groupId>com.alibaba</groupId>
        <artifactId>druid</artifactId>
        <version>${druid.version}</version>
    </dependency>

    <!--lombok依赖-->
    <dependency>
        <groupId>org.projectlombok</groupId>
        <artifactId>lombok</artifactId>
        <version>${lombok.version}</version>
        <scope>provided</scope>
    </dependency>

    <!--junit依赖-->
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>${junit.version}</version>
        <scope>test</scope>
    </dependency>
    <!-- log4j日志依赖 -->
    <dependency>
        <groupId>log4j</groupId>
        <artifactId>log4j</artifactId>
        <version>${log4j.version}</version>
    </dependency>
    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-api</artifactId>
        <version>${slf4j.version}</version>
    </dependency>
    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-log4j12</artifactId>
        <version>${slf4j.version}</version>
    </dependency>
</dependencies>

2.建立User类

image.png

建立UserDAO与BaseDAO

image.png
image.png

建立UserService和UserServiceImpl类

image.png
image.png

3.配置spring_mybatis.xml文件

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd">

<!--读入外部数据库连接属性文件-->
<context:property-placeholder location="classpath:jdbc.properties"/>

<!--扫描Service包的组件-->
<context:component-scan base-package="com.spring.orm.service"/>

<!--通过druid配置数据源-->
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource"
      init-method="init" destroy-method="close">
    <property name="driverClassName" value="${jdbc.driverClassName}"/>
    <property name="url" value="${jdbc.url}"/>
    <property name="username" value="${jdbc.username}"/>
    <property name="password" value="${jdbc.password}"/>
    <!-- 初始化大小 -->
    <property name="initialSize" value="20"/>
    <!-- 连接池最大使用连接数量 -->
    <property name="maxActive" value="20"/>
    <!-- 连接池最小空闲 -->
    <property name="minIdle" value="0"/>
    <!-- 配置获取连接等待超时的时间 -->
    <property name="maxWait" value="60000"/>
    <!-- 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒 -->
    <property name="timeBetweenEvictionRunsMillis" value="60000"/>
    <!-- 配置一个连接在池中最小生存的时间,单位是毫秒 -->
    <property name="minEvictableIdleTimeMillis" value="300000"/>
    <!-- 连接空闲时测试是否有效 -->
    <property name="testWhileIdle" value="false"/>
    <!-- 获取连接时测试是否有效 -->
    <property name="testOnBorrow" value="false"/>
    <!-- 归还连接时是否测试有效 -->
    <property name="testOnReturn" value="false"/>
    <!-- 打开PSCache缓存,并且指定每个连接上PSCache的大小 -->
    <property name="poolPreparedStatements" value="true"/>
    <property name="maxPoolPreparedStatementPerConnectionSize" value="20"/>
</bean>

<!-- 配置mybatis的Session -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
    <property name="dataSource" ref="dataSource"/>
    <property name="typeAliasesPackage" value="com.spring.orm.entity"/>
</bean>

<!-- 配置通用Mapper -->
<bean class="tk.mybatis.spring.mapper.MapperScannerConfigurer">
    <property name="basePackage" value="com.spring.orm.dao"/>
    <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
    <property name="markerInterface" value="com.spring.orm.dao.BaseDAO"/>
    <property name="properties">
        <value>
            mappers = com.spring.orm.dao.BaseDAO
            IDENTITY = MYSQL
        </value>
    </property>
</bean>

<!--事务管理bean -->
<bean id="manager"
      class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
    <property name="dataSource" ref="dataSource"/>
</bean>
<!-- 使用声明式事务 -->
<tx:annotation-driven transaction-manager="manager"/>

</beans>

4.编写测试类进行测试

public class UserServiceImplTest {

@Autowired private UserService userService;
@Test
public void insertUser() {
    User user = new User();
    user.setAccount("test");
    user.setPassword("8888888");
    user.setCredits(999);
    int n = userService.insertUser(user);
    assertEquals(1, n);
}

@Test
public void deleteUser() {
    int n = userService.deleteUser(3);
    assertEquals(1,n);
}

@Test
public void updateUser() {
    User user = userService.getUser(2);
    user.setPassword("000");
    user.setCredits(500);
    int n = userService.updateUser(user);
    assertEquals(1, n);
}

@Test
public void selectUsers() {
    List<User> users = userService.selectUsers();
    users.forEach(user -> System.out.println(user));
}

@Test
public void getUser() {
    User user = userService.getUser(2);
    System.out.println(user);
}

}

上一篇下一篇

猜你喜欢

热点阅读