频道栏目
首页 > 程序开发 > 软件开发 > Java > 正文
Maven + Spring MVC+Mybatis + MySQL +AngularJS + Bootstrap 实现前后台交互页面(二)访问数据库
2016-08-02 09:15:49         来源:shymi1991的专栏  
收藏   我要投稿

半年过去了,为了对得起看过(一)的三百多位朋友和我自己,还是得要把这系列写完。AnyWay,迟做总比不做好。

这一节我们将整合spring、mybatis成功连接数据库并可以进行表的增删改查。

1. 准备所需要的pom依赖


	4.0.0
	com.sommer.sample
	webproject
	war
	0.0.1-SNAPSHOT
	webproject Maven Webapp
	https://maven.apache.org
	
		
		4.2.4.RELEASE
		
		3.4.1
	
	
		
			junit
			junit
			4.12
			test
		
		
			org.mybatis
			mybatis
			3.4.1
		
		
			org.mybatis
			mybatis-spring
			1.3.0
		
		
			org.springframework
			spring-core
			${spring.version}
		
		
			org.springframework
			spring-beans
			${spring.version}
		
		
		
			org.springframework
			spring-context
			${spring.version}
		
		
			org.springframework
			spring-oxm
			${spring.version}
		
		
			org.springframework
			spring-tx
			${spring.version}
		
		
			org.springframework
			spring-web
			${spring.version}
		
		
			org.springframework
			spring-webmvc
			${spring.version}
		
		
			org.springframework
			spring-jdbc
			4.2.4.RELEASE
		
		
			org.springframework
			spring-context-support
			${spring.version}
		
		
			org.springframework
			spring-test
			${spring.version}
		
		
		
			javax
			javaee-api
			7.0
		
		
		
			commons-dbcp
			commons-dbcp
			1.4
		
		
		
			mysql
			mysql-connector-java
			6.0.2
		
		
		
			com.fasterxml.jackson.core
			jackson-databind
			2.5.4
		
		
			com.fasterxml.jackson.core
			jackson-core
			2.5.4
		
		
			com.fasterxml.jackson.core
			jackson-annotations
			2.5.4
		
	
	
		webproject
	

2. 配置文件

\

spring-mvc是web层的配置,我们放到下一节。

2.1 数据库连接配置

为了方便管理和修改,我们将这些信息放在jdbc.properties里面

#database

driver=com.mysql.cj.jdbc.Driver
jdbc.url=jdbc:mysql://127.0.0.1:3306/sample?serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8&useSSL=false
jdbc.username=root
jdbc.password=123456
jdbc.maxWait=5000

com.mysql.cj.jdbc.Driver是mysql-connector-java 6.2的新驱动名称,老的是com.mysql.jdbc.Driver。

useUnicode、characterEncoding指定字符的编码、解码格式;serverTimezone为了让server(tomcat)和mysql的时区同步,不加的话会可能会报下面的错,这是mysql-connector-java从5.1.33版本后TimeUtil类存在bug。mysql新的安全性设置要求SSL连接,所以加上参数userSSL=false。

Cannot create PoolableConnectionFactory
 (The server time zone value '?й???????' is unrecognized or represents more than one time zone. 
You must configure either the server or JDBC driver
 (via the serverTimezone configuration property) 
to use a more specifc time zone value if you want to utilize time zone support.)

2.2 spring-mybatis整合


    
    

    
    
        
            
                classpath*:conf/jdbc.properties
            
        
    

    
    
        
        
        
        
        
 		

    

    
    
    	
    	
    

    
    
    	
    	
    



3. Java 代码

Java代码分成实体层entity、数据访问层dao、业务服务层service(又分为接口和实现)和控制层controller。控制层是与前端交互打交道的,可以先不管。

\
 

package com.sommer.sample.dashboard.entity;

public class Author {
	private int id;
	private String name;
	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;
	}
	
}

package com.sommer.sample.dashboard.dao;

import java.util.List;

import org.apache.ibatis.annotations.Param;

import com.sommer.sample.dashboard.entity.Author;

public interface AuthorMapper {
	List findList(@Param("id")int id);
}

package com.sommer.sample.dashboard.service;

import java.util.List;

import com.sommer.sample.dashboard.entity.Author;

public interface AuthorService {
	List findList(int id);
}

package com.sommer.sample.dashboard.service.impl;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.sommer.sample.dashboard.dao.AuthorMapper;
import com.sommer.sample.dashboard.entity.Author;
import com.sommer.sample.dashboard.service.AuthorService;

@Service
public class AuthorServiceImpl implements AuthorService{

	@Resource
	private AuthorMapper authorMapper;
	
	public List findList(int id) {
		return authorMapper.findList(id);
	}

}

4. SQL映射的xml文件

\



	
		
		
	
	

这里要注意的是映射文件的头部,mybatis的配置文件是:

...

mapper.xml文件的头部是


...

doctype定义写错的话eclipse一般会提示的。

5. 单元测试:

package webproject;

import java.util.List;

import javax.annotation.Resource;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import com.sommer.sample.dashboard.entity.Author;
import com.sommer.sample.dashboard.service.AuthorService;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations={"classpath:conf/spring-mybatis.xml"})
public class AuthorTest {
	@Resource
	AuthorService as;
	@Test
	public void testFindList(){
		List res= as.findList(1);
		System.out.println(res.get(0).getName());
		
	}
}

执行单元测试,打印出

sommer

同理我们加上插入、删除和更新的功能:

在authorMapper.xml中加上

   
		delete from author where id = #{id}
	
	
		insert into author (name)values(#{name})
	
	
		update author set name=#{name} where id =#{id}
	
AuthorMapper.java
public interface AuthorMapper {
	List findList(@Param("id")int id);
	Integer delete(@Param("id")int id);
	Integer add(Author author);
	Integer update(Author author);
}

AuthorService.java
public interface AuthorService {
	List findList(int id);
	Integer delete(int id);
	Integer add(Author author);
	Integer update(Author author);
}

AuthorServiceImpl.java
@Service
public class AuthorServiceImpl implements AuthorService{

	@Resource
	private AuthorMapper authorMapper;
	
	public List findList(int id) {
		return authorMapper.findList(id);
	}

	public Integer delete(int id) {
		return authorMapper.delete(id);
	}

	public Integer add(Author author) {
		return authorMapper.add(author);
	}

	public Integer update(Author author) {
		return authorMapper.update(author);
	}

}

单元测试AuthorTest.java
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations={"classpath:conf/spring-mybatis.xml"})
public class AuthorTest {
	@Resource
	AuthorService as;
	@Test
	public void testFindList(){
		List res= as.findList(1);
		System.out.println(res.get(0).getName());
	}
	@Test
	public void testDelete(){
		Integer res= as.delete(1);
		System.out.println(res);
	}
	@Test
	public void testAdd(){
		Author author = new Author();
		author.setName("Spring");
		Integer res = as.add(author);
		//return primary key of the insert object
		System.out.println(author.getId());
	}
	@Test
	public void testUpdate(){
		Author author = new Author();
		author.setId(2);
		author.setName("Autumn");
		Integer res = as.update(author);
		System.out.println(res);
	}
}
点击复制链接 与好友分享!回本站首页
相关TAG标签 页面 数据库
上一篇:用HSQLDB数据库测试Hibernate和Spring
下一篇:[Java]struts,spring,svn杂记
相关文章
图文推荐
点击排行

关于我们 | 联系我们 | 广告服务 | 投资合作 | 版权申明 | 在线帮助 | 网站地图 | 作品发布 | Vip技术培训 | 举报中心

版权所有: 红黑联盟--致力于做实用的IT技术学习网站