1.新建一个Java dynamic web项目。
2.导入包包。
Springmvc部分:
3.在web.xml 中配置dispatcherservlet,申明项目是基于pringmvc的。
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
4.在springmvc-servlet.xml 中配置HandlerMapping 的映射方式,用注释是最简单的。
<!--springmvc 注解驱动 -->
<mvc:annotation-driven/>
<!-- 扫描器 -->
<context:component-scan base-package="com"></context:component-scan>
<!-- 配置视图解析器 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<!-- 前缀 -->
<property name="prefix" value="/view/"></property>
<!-- 后缀 -->
<property name="suffix" value=".jsp"></property>
</bean>
5.新建一个controller.java,位置在‘扫描器’定义的包中
@Controller
public class HelloController {
@RequestMapping("/hi.do")
public String sayhi() {
System.out.println("hi");
return "hello";
}
}
6.新建首页index.jsp
<form action="hi.do">
Hi <input type="submit"/>
</form>
7.新建响应页面hello.jsp,位置根据‘视图解析器’
Mybatis部分:
1.新建一个实体类:添加setter,getter,toSring,有参,无参构造方法
public class User {
private int id;
private String name;
private Date birthday;
private double salary;
}
2.在数据库中建立对应的表
CREATE TABLE s_user(
user_id INT AUTO_INCREMENT PRIMARY KEY,
user_name VARCHAR(30),
user_birthday DATE,
user_salary DOUBLE
)
3.在beans.xml配置数据源,mapper等信息
<?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:p="http://www.springframework.org/schema/p"
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-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/tx
http://www.springframework.org/schema/tx/spring-tx-3.2.xsd">
<!-- 1. 数据源 : DriverManagerDataSource -->
<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/mybatis" />
<property name="username" value="root" />
<property name="password" value="root" />
</bean>
<!-- 2. mybatis 的SqlSession 的工厂: SqlSessionFactoryBean -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="typeAliasesPackage" value="com.wjp.domain" />
</bean>
<!-- 3. mybatis 自动扫描加载Sql 映射文件 : MapperScannerConfigurer -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<span style="color:#ff6666;"><property name="basePackage" value="com.wjp.mapper" /></span>
<property name="sqlSessionFactory" ref="sqlSessionFactory" />
</bean>
<!-- 4. 事务管理 : DataSourceTransactionManager -->
<bean id="txManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource" />
</bean>
<!-- 5. 使用声明式事务 -->
<tx:annotation-driven transaction-manager="txManager" />
</beans>
4.新建UserMapper.java
public interface UserMapper {
void save(User user);
void delete(User user);
void update(User user);
User findById(int id);
List<User> findAll();
}
5.新建对应的UserMapper.xml. 文件名必须与Java文件相同,spring才能注入。其中namespace为UserMapper.java的全路径,SQL元素的id必须和接口方法名一致。
<?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.wjp.mapper.UserMapper">
<insert id="save" keyProperty="id" keyColumn="user_id" useGeneratedKeys="true">
insert into s_user(user_name,user_birthday,user_salary)
values (#{name},#{birthday},#{salary})
</insert>
<delete id="delete">
delete from s_user where id=#{id}
</delete>
<update id="update">
update s_user set
user_name = #{name}
user_birthday = #{birthday}
user_salary = #{salary}
where id = #{id}
</update>
<select id="findById" resultMap="user">
select * from s_user where id=#{id}
</select>
<select id="findAll" resultMap="user">
select * from s_user
</select>
<resultMap type="User" id="user">
<result property="id" column="user_id"/>
<result property="name" column="user_name"/>
<result property="birthday" column="user_birthday"/>
<result property="salary" column="user_salary"/>
</resultMap>
</mapper>
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("<span style="color:#ff6666;">/config/beans.xml</span>")
public class TestSql {
@Autowired
private UserMapper userMapper;
@Test
public void save() {
User user = new User();
Date birthday = new Date();
user.setBirthday(birthday);
user.setName("haha");
user.setSalary(200000);
userMapper.save(user);
System.out.println(user.getId());
}
}
结果:
测试Springmvc+mybatis:
1.前端:
<form action="user/save.do">
User save <input type="submit"/>
</form>
2.新建usercontroller
@Controller
@RequestMapping("/user")
public class UserController {
@Autowired
private UserMapper usermapper;
@RequestMapping("/save.do")
public String save() {
System.out.println("saving");
User user = new User();
Date birthday = new Date();
user.setBirthday(birthday);
user.setName("hehe");
user.setSalary(233);
usermapper.save(user);
return "hello";
}
}
结果: