你的位置:首页 > 软件开发 > Java > 2 将mybatis配置到springmvc中

2 将mybatis配置到springmvc中

发布时间:2017-11-15 12:00:14
为了更方便的连接数据库,将mybatis配置到springMVC中1). 首先是jar包 多了3个jar druid 这个是阿里的数据库连接包 mybatis和 mybatis-spring 2) 然后是项目目录 ...

2 将mybatis配置到springmvc中

为了更方便的连接数据库,将mybatis配置到springMVC中

1).  首先是jar包    多了3个jar  druid 这个是阿里的数据库连接包      mybatis和 mybatis-spring  

2 将mybatis配置到springmvc中

 

2)  然后是项目目录

2 将mybatis配置到springmvc中

 

3)在web.

<context-param></context-param>元素含有一对参数名和参数值,用作应用的servlet上下文初始化参数。参数名在整个Web应用中必须是惟一的。设定web应用的环境参数(context)

2 将mybatis配置到springmvc中

4)

  spring-mvc的内容不变,spring-mybatis中的内容如下

<!-- MyBatis配置 这个就是spring和mybatis的整合 也就是spring-mybatis jar-->
<bean id="mysqlSqlSessionFactory" >
<!--数据库 多数据源配置多个-->
<property name="dataSource" ref="mysqlDataSource" />
<!-- 自动扫描mapping.
<!-- 自动扫描entity目录, 省掉 该属性可以给包中的类注册别名,注册后可以直接使用类名,而不用使用全限定的类名-->
<property name="typeAliasesPackage" value="com.springmvc.model" />
<!-- mysqlSqlSessionFactory会自动扫描该路径下的所有文件并解析。-->
<property name="mapperLocations">
<list>
<value>classpath:/mybatis/*Mapper. </list>
</property>
</bean>

<!--会查找类路径下的映射器并自动将它们创建成MapperFactoryBean  -->
<bean >
<property name="sqlSessionFactoryBeanName" value="mysqlSqlSessionFactory"></property>

<!-- 为映射器接口文件设置基本的包路径 -->
<property name="basePackage" value="com.springmvc.dao" />
<!-- 该属性起到一个过滤的作用,设置该属性,那么mybatis的dao接口 只有包含该注解 才会被扫描-->
<property name="annotationClass" value="com.springmvc.base.JYBatis"/>
</bean>

 

5) 自定义的JYBatis

2 将mybatis配置到springmvc中

 

 

/**
* 标识MyBatis的DAO,方便{@link org.mybatis.spring.mapper.MapperScannerConfigurer}的扫描�??
*
* 总的来说就是 target(接口) retention(java-class后依旧可用) document(包含在javadoc中) component(spring扫描)
*/
@Retention(RetentionPolicy.RUNTIME) //注解的生命周期 这个是最长的 jvm加载class文件之后,仍然存在
@Target(ElementType.TYPE) //注解修改目标 (这是个接口) 接口、类、枚举、注解
@Documented //该注解将被包含在javadoc中
@Component //@Component泛指组件,当组件不好归类的时候,我们可以使用这个注解进行标注。
public @interface JYBatis {
  String value() default "";
}

 6) 数据库连接参数 (这个根据自己本地的库的名字和端口 来自己写)

db.username=root
db.password=123456
db.url=jdbc:mysql://localhost:3306/test?useUnicode=true&amp;characterEncoding=utf-8
db.dirverClass=com.mysql.jdbc.Driver

 

这样mybatis就整合到springmvc中了,下面做一个例子,往mysql中插入一条数据  

 

1) 首先是jsp页面  

2 将mybatis配置到springmvc中

 

 还在login.jsp中写一个form

<form action="spring/student/testController" method="post">
  <br />用户名: <input type="text" name="name"> <br />

  <br />年龄: <input type="text" name="age"> <br />

  <br /> 老师: <input type="text" name="teacher">
  <br />  <input type="submit" value="登录">
</form>

 

2) model类    然后写一个Student model类

//Alias是mybatis给当前model类起的别名 typeAlias
@Alias("Student")
public class Student {
private int id;
private String name;
private int age;
private String teacher;

 

3)StudentController类

@Controller
@RequestMapping("/spring/student")
public class StudentController {
@Resource
private StudentService ss;

@RequestMapping(value="/testController")
public String toPage(Student s){
System.out.println(s.toString());
s.setId(33);
ss.save(s);
return "success";
}

}

 

 4) StudentService    StudentServiceImpl  StudentDao

 

public interface StudentService {

public void save(Student student);
}

 //StudentServiceImpl 这里要加上注解

@Service("StudentService") 
public class StudentServiceImpl implements StudentService {

@Autowired
private StudentDao studentDao;

@Override
public void save(Student student) {
studentDao.insert(student);
}

 

 StudentDao  要加上自定义注解  这里spring会自动为其创建bean

@JYBatis
public interface StudentDao {

public void insert(Student student);

}

 

 5)  最后是mybatis的

<?<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.springmvc.dao.StudentDao">
<!-- com.jy.entity.system.account.Account -->
<!-- com.jy.entity.oa.leave.Leave -->
<resultMap id="base" type="Student" > </resultMap>

<select id="find" resultMap="base" parameterType="Student">
  SELECT t.* FROM user1 t WHERE 1=1
<if test="id != null and id!='' ">
  AND t.id=#{id}
</if>
</select>


<select id="count" resultType="int" parameterType="Student">
  SELECT count(*) FROM user1 t WHERE 1=1
</select>
<insert id="insert" parameterType="Student">
<![CDATA[
INSERT INTO user1(
  id,
  age,
  name,
  teacher
  ) VALUES (
  #{id},
  #{age},
  #{name},
  #{teacher}
  )
]]>
</insert>

<update id="updateUserAssetInfo" parameterType="Map">
  UPDATE user1
  SET
  id=#{id},
  age=#{age},
  name=#{name},
  teacher=#{teacher}
  WHERE id=#{id}
</update>

</mapper>

 

 

 


原标题:2 将mybatis配置到springmvc中

关键词:Spring

*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们: admin#shaoqun.com (#换成@)。

可能感兴趣文章

我的浏览记录