正式开始配置关键内容,这是硬货
1、新建spring配置文件,起名为 applicationContext.xml,放在src/main/resources/spring目录下。
内容如下:
<?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:jdbc="http://www.springframework.org/schema/jdbc"
xmlns:jee="http://www.springframework.org/schema/jee"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:jpa="http://www.springframework.org/schema/data/jpa"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.2.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.2.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd">
<!--开始配置-->
</beans>
2、web.xml文件配置
<context-param>
<param-name>contextConfigLocation</param-name>
<!-- 上面配置的spring配置文件的路径,区分大小写 -->
<param-value>classpath:spring/applicationContext.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
加入这个用来在启动web容器时,读取spring配置
3.新建数据源的配置文件datasource.properties放在src/main/resources目录下:
driverClassName=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/user?characterEncoding=utf-8
username=root
password=1234
4.配置一个duird数据源,在applicationContext.xml中加入:
<!--开始配置-->
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations" value="classpath:datasource.properties" />
</bean>
<!-- 配置数据源 -->
<bean id="ds" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close">
<property name="url" value="${url}"/>
<property name="driverClassName" value="${driverClassName}"/>
<property name="username" value="${username}"/>
<property name="password" value="${password}"/>
</bean>
5.配置Mybatis
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="mapperLocations" value="classpath*:mappers/*Mapper.xml"></property>
</bean>
<bean name="mapperScannerConfigurer" class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.jczj123.Test3.dao"/>
</bean>
6.配置就这么多,下面是代码
model下新建User实体类
package com.jczj123.Test3.model;
/**
* Created by wangkai on 2017/7/9.
*/
public class User {
private Integer id;
private String username;
private String password;
public User(Integer id, String username, String password) {
this.id = id;
this.username = username;
this.password = password;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
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;
}
}
在dao下新建一个UserMapper
package com.jczj123.Test3.dao;
import com.jczj123.Test3.model.User;
import java.util.List;
/**
* Created by wangkai on 2017/7/9.
*/
public interface UserMapper {
User selectById(int id);
List<User> selectAll();
}
在src/main/resources下新建目录mappers
新建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.jczj123.Test3.dao.UserMapper" >
<resultMap id="BaseResultMap" type="com.jczj123.Test3.model.User" >
<constructor >
<idArg column="id" jdbcType="INTEGER" javaType="java.lang.Integer" />
<arg column="username" jdbcType="VARCHAR" javaType="java.lang.String" />
<arg column="password" jdbcType="VARCHAR" javaType="java.lang.String" />
</constructor>
</resultMap>
<sql id="Base_Column_List" >
id, username, password
</sql>
<select id="selectById" resultMap="BaseResultMap" parameterType="java.lang.Integer" >
select
<include refid="Base_Column_List" />
from user
where id = #{id,jdbcType=INTEGER}
</select>
<select id="selectAll" resultMap="BaseResultMap">
select
<include refid="Base_Column_List" />
from user
</select>
</mapper>
service包下新建IUserService接口
package com.jczj123.Test3.service;
import com.jczj123.Test3.model.User;
import java.util.List;
/**
* Created by wangkai on 2017/7/9.
*/
public interface IUserService {
User findById(int id);
List<User> findAll();
}
service包下新建子包impl
新建实现类
UserServiceImpl
package com.jczj123.Test3.service.impl;
import com.jczj123.Test3.dao.UserMapper;
import com.jczj123.Test3.model.User;
import com.jczj123.Test3.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
/**
* Created by wangkai on 2017/7/9.
*/
@Service(value = "userService")
public class UserServiceImpl implements IUserService {
@Autowired
private UserMapper userMapper;
public User findById(int id) {
return userMapper.selectById(id);
}
public List<User> findAll() {
return userMapper.selectAll();
}
}
controller下新建一个UserController
package com.jczj123.Test3.controller;
import com.jczj123.Test3.model.User;
import com.jczj123.Test3.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import java.util.List;
/**
* Created by wangkai on 2017/7/9.
*/
@Controller
public class UserController {
@Autowired
private IUserService userService;
@RequestMapping("/list.do")
public String doGet(ModelMap modelMap) {
List<User> userList = userService.findAll();
modelMap.addAttribute("users", userList);
return "userList";
}
}
velocity下新建userList.vm文件
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>用户列表</title>
</head>
<body>
<table border="1">
<tr>
<th>用户id</th>
<th>用户名</th>
<th>密码</th>
</tr>
#foreach($user in $users)
<tr>
<td>$user.id</td>
<td>$user.username</td>
<td>$user.password</td>
</tr>
#end
</table>
</body>
</html>
启动tomcat服务器,浏览器访问:
就可以看到:
表示成功了
代码托管在码云上,地址是:
http://git.oschina.net/nicekk/spring-mybatis-simple
欢迎访问