原标题:java搭建SSM开发环境
1.添加spring支持,加入mybatis jar包
2.配置web.xml文件.
在web.xml中加入以下代码,用于读取beans.xml文件和处理乱码
controller
org.springframework.web.servlet.DispatcherServlet
contextConfigLocation
classpath:beans.xml
controller
*.action
encoding
org.springframework.web.filter.CharacterEncodingFilter
encoding
UTF-8
encoding
/*
3.配置beans.xml文件
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:tx="http://www.springframework.org/schema/tx"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.1.xsd
"
>
4.编写数据访问层
1).编写接口,
2).编写mybatis的xml文件.
/p>
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
select t.*,s.id sid,s.name sname,s.sex ssex,s.age sage from teacher t left join student s on t.id=s.tid
select s.*,t.id tid,t.name tname,t.sex tsex,t.age tage from student s inner join teacher t on s.tid=t.id
update student
name=#{name},
age=#{age},
where id=#{id}
insert into teacher (name,sex,age)values(#{name},#{sex},#{age})
其中有几点需要注意的
resultMap节点,用于设置返回数据和实体类的对应,里面有两个节点,type,id
操作节点,类似select,update,insert,操作这些节点的时候sql语句自己的属性就直接写,参数的属性就写#{}里面
update里面有一个set标签,记住更新的用法
联表查询,注意如果两张表数据库字段如果是一样的话,应该用别名处理,具体上面有。
springMVC和mybatis的集成基本实现。返回搜狐,查看更多
责任编辑: