返回顶部

收藏

spring + mybatis + mysql入门实例

更多

准备:

安装 : STS 2.9.1.RELEASE, mysql等

在STS中新建一个spring template project - spring MVC project即可

1、 创建测试使用的数据表

create table user   
(  
  id int NOT NULL primary key auto_increment,   
  name varchar(20) NOT NULL,   
  age int NOT NULL  
);  
insert into user (name, age) values ("name1", 11)  

2、 POJO实体类定义

package cn.outofmemory.test.dao;  

import java.io.Serializable;  

public class User implements Serializable {  

    private static final long serialVersionUID = -433577178466098132L;  
    private int id;  
    private String name;  
    private int age;  

    public int getId() {  
        return id;  
    }  
    public void setId(int id) {  
        this.id = id;  
    }  
    public String getName() {  
        return name;  
    }  
    public void setName(String name) {  
        this.name = name;  
    }  
    public int getAge() {  
        return age;  
    }  
    public void setAge(int age) {  
        this.age = age;  
    }  
}  

3、 mapper(使用注解配置sql,不用xml配置文件)

package cn.outofmemory.test.dao;  

import java.util.List;  

import org.apache.ibatis.annotations.Insert;  
import org.apache.ibatis.annotations.Param;  
import org.apache.ibatis.annotations.Select;  

public interface UserMapper {  

    @Insert("INSERT into user (name, age) VALUES (#{name}, #{age})")  
    public void insertUser(User user);  

    @Select("SELECT * from user  WHERE id = #{id}")  
    public User getUser(@Param("id") int id);  

    @Select("SELECT * from user")  
    public List<User> getAllUser();  
}  

4 - mybatis-context.xml 配置相关的bean

<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">    
    <property name="driverClassName" value="com.mysql.jdbc.Driver"/>  
    <property name="url" value="jdbc:mysql://localhost:3306/mytest" />  
    <property name="username" value="root" />  
    <property name="password" value="admin" />  
</bean>    

<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">    
    <property name="dataSource" ref="dataSource" />    
</bean>    

<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">    
    <property name="dataSource" ref="dataSource" />    
</bean>    

<!-- mapper bean -->    
<bean id="userMapper" class="org.mybatis.spring.mapper.MapperFactoryBean">    
    <property name="mapperInterface" value="cn.outofmemory.test.dao.UserMapper" />    
    <property name="sqlSessionFactory" ref="sqlSessionFactory" />    
</bean>  

5、 修改web.xml,加上mybatis-context

<servlet>  
    <servlet-name>appServlet</servlet-name>  
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>  
    <init-param>  
        <param-name>contextConfigLocation</param-name>  
        <param-value>  
            /WEB-INF/spring/appServlet/servlet-context.xml,  
            /WEB-INF/classes/mybatis-context.xml,  
        </param-value>  
    </init-param>  
    <load-on-startup>1</load-on-startup>  
</servlet>  

6、 修改pom.xml添加mybatis和mysql依赖的jar包

<!-- mybatis -->   
<dependency>  
  <groupId>org.mybatis</groupId>  
  <artifactId>mybatis-spring</artifactId>  
  <version>1.1.1</version>  
</dependency>   

<!-- mysql -->  
<dependency>    
   <groupId>mysql</groupId>    
   <artifactId>mysql-connector-java</artifactId>    
   <version>5.1.21</version>    
</dependency>  

7、 测试代码

List<User> users = userMapper.getAllUser();  
for(User u : users) {  
    System.out.println(u.getName()+", id >> "+u.getId());  
}  
System.out.println("----------insert before-------");  
User u = new User();  
u.setName("insertUser");  
u.setAge(22);  
userMapper.insertUser(u);  
System.out.println("----------insert end-------");  

标签:spring,mybatis

收藏

0人收藏

支持

1

反对

0

发表评论