Spring+MyBatis

IDE用的是Idea,正好头一阵打折买的,熟悉一下,顺便再熟悉一下maven

pom如下:

<?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.prism</groupId>
    <artifactId>prism</artifactId>
    <version>0.0.1</version>

    <dependencies>
        <!--MyBatis-->
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis</artifactId>
            <version>3.3.0</version>
        </dependency>
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis-spring</artifactId>
            <version>1.2.3</version>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.29</version>
        </dependency>
        <!--JUnit-->
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.11</version>
        </dependency>
        <!--log4j-->
        <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>1.2.17</version>
        </dependency>

        <dependency>
            <groupId>javax.annotation</groupId>
            <artifactId>javax.annotation-api</artifactId>
            <version>1.2</version>
        </dependency>
        <dependency>
            <groupId>javax.transaction</groupId>
            <artifactId>javax.transaction-api</artifactId>
            <version>1.2</version>
        </dependency>

        <!--Spring-->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-orm</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-oxm</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-tx</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-jdbc</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-test</artifactId>
        </dependency>
        <!--spring-oxm依赖-->
        <dependency>
            <groupId>org.codehaus.castor</groupId>
            <artifactId>castor-xml</artifactId>
            <version>1.3.3</version>
        </dependency>
        <!--spring-json依赖-->
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-databind</artifactId>
            <version>2.4.2</version>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.dataformat</groupId>
            <artifactId>jackson-dataformat-xml</artifactId>
            <version>2.4.2</version>
        </dependency>
        <!--Spring-aop 依赖-->
        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjweaver</artifactId>
            <version>1.8.2</version>
        </dependency>
        <!--druid-->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid</artifactId>
            <version>1.0.11</version>
        </dependency>
    </dependencies>
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-framework-bom</artifactId>
                <version>4.1.2.RELEASE</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>
</project>

其中有一些用不到的

entity user :

public class User {
    private Long id;
    private String name;
    private Integer age;

    public User(String name,Integer age){
        this.name = name;
        this.age = age;
    }
    public User(Long id,String name,Integer age){
        this.id = id;
        this.name = name;
        this.age = age;
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

    @Override
    public String toString() {
        return " user : [ id : "+id+" , name : "+name+" , age : "+age+"]";
    }

UserMapper 如下:

public interface UserMapper {

    User selectUser(Integer id);

    void saveUser(User user);
}

UserMapper.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.prism.mapper.UserMapper">
    <select id="selectUser" resultType="user">
        select * from my_user where id = #{id}
    </select>
       <!-- keyProperty="id" 和useGeneratedKeys="true"作用为插入后返回自增主键 -->
       <!-- 之前没配useGeneratedKeys的时候也成功返回了主键,使用的版本是3.2.1 
           但是那时候是直接用sqlSessionTemplate调用的namespace.id那种方式的,不知道是不是这个原因-->
       <!-- 还有另外一种形式,使用selectKey的方式 -->
    <insert id="saveUser"  parameterType="user" keyProperty="id" useGeneratedKeys="true">
        <!--<selectKey resultType="long" keyProperty="id">
            select last_insert_id()
        </selectKey>-->
        insert into my_user (name,age) values (#{name},#{age})
    </insert>
</mapper>

这里需要注意一下,mapper的namespace属性

namespace的值要等于UserMapper接口的全限定类名

我这边是这样的,不确认是不是因为配置有问题,导致的这种结果


接下来就是ApplicationContext了

<?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:aop="http://www.springframework.org/schema/aop"
       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/aop
       http://www.springframework.org/schema/aop/spring-aop.xsd
       http://www.springframework.org/schema/tx
       http://www.springframework.org/schema/tx/spring-tx.xsd
       ">

    <context:component-scan base-package="com.prism.service"></context:component-scan>

    <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close">
        <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
        <property name="url" value="jdbc:mysql://localhost:3306/mybatis_test"/>
        <property name="username" value="mybatis"/>
        <property name="password" value="123456"/>
    </bean>

    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource"/>
        <!-- Mapper.xml文件的位置 -->
        <property name="mapperLocations">
            <array>
                <value>classpath:mapper/*.xml</value>
            </array>
        </property>
        <!-- 实体类别名,会将指定包下的实体类映射别名,别名为实体类首字母小写的类名, -->
        <!-- 相当于mybatis-config.xml里面的typeAliases -->
        <property name="typeAliasesPackage" value="com.prism.entity"/>
    </bean>
    
    <!-- 扫描mapper接口文件,与xml映射 -->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="addToConfig" value="true"/>
        <property name="basePackage" value="com.prism.mapper"/>
    </bean>

    <bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate" scope="prototype">
        <constructor-arg index="0" ref="sqlSessionFactory"/>
    </bean>
</beans>

UserService如下:

@Component
public class UserService {
    @Autowired
    private UserMapper userMapper;

    public User selectUser(Integer id){
        return userMapper.selectUser(id);
    }

    public void saveUser(User user){
        userMapper.saveUser(user);
    }
}

最后就是Junit的Test类了

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext.xml")
public class Test {
    
    @Autowired
    private UserService userService;
  

    @org.junit.Test
    public void testUser(){
        System.out.println(userService.selectUser(1).toString());
    }
    
    @org.junit.Test
    public void testSaveUser(){
        User user = new User("oldCat",22);
        userService.saveUser(user);
        System.out.println(user.toString());
    }
}


你可能感兴趣的:(Spring+MyBatis)