标签:
阅读本文之前建议先阅读如下两篇文章
一、创建数据库和表
先创建数据库wangci,再创建一个表user(仅仅测试用),完成之后随意插几条数据进去。
CREATE TABLE `user` ( `id` bigint(20) NOT NULL AUTO_INCREMENT, `username` varchar(20) NOT NULL, `password` varchar(20) NOT NULL, PRIMARY KEY (`id`,`username`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8
二、pom.xml中加入Mybatis和MySQL相关依赖
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>wang.wangci</groupId> <artifactId>bottae-parent</artifactId> <packaging>pom</packaging> <version>1.0</version> <name>bottae-parent</name> <url>http://maven.apache.org</url> <modules> <module>bottae-common</module> <module>bottae-dao</module> <module>bottae-service</module> <module>bottae-web</module> </modules> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <spring.version>3.2.3.RELEASE</spring.version> </properties> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.0</version> <configuration> <source>1.6</source> <target>1.6</target> <encoding>${project.build.sourceEncoding}</encoding> </configuration> </plugin> <!-- jetty插件 --> <plugin> <groupId>org.mortbay.jetty</groupId> <artifactId>jetty-maven-plugin</artifactId> <version>8.1.11.v20130520</version> <configuration> <scanIntervalSeconds>0</scanIntervalSeconds> <connectors> <connector implementation="org.eclipse.jetty.server.nio.SelectChannelConnector"> <port>7777</port> </connector> </connectors> </configuration> </plugin> </plugins> <resources> <resource> <directory>src/main/resources</directory> <filtering>true</filtering> </resource> </resources> </build> <dependencies> <!-- spring start --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-web</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context-support</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-tx</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-beans</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-orm</artifactId> <version>${spring.version}</version> </dependency> <!-- spring 依賴jackson --> <dependency> <groupId>org.codehaus.jackson</groupId> <artifactId>jackson-mapper-asl</artifactId> <version>1.9.7</version> </dependency> <!-- spring end --> <dependency> <groupId>javax</groupId> <artifactId>javaee-api</artifactId> <version>6.0</version> <scope>provided</scope> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.25</version> </dependency> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis-spring</artifactId> <version>1.1.1</version> </dependency> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.1.1</version> </dependency> <dependency> <groupId>com.mchange</groupId> <artifactId>c3p0</artifactId> <version>0.9.5-pre8</version> </dependency> </dependencies> <profiles> <profile> <id>DEV</id> <properties> <dao.jdbc.url>10.236.121.48</dao.jdbc.url> <dao.jdbc.port>3306</dao.jdbc.port> <dao.jdbc.schema>wangci</dao.jdbc.schema> <dao.jdbc.user>root</dao.jdbc.user> <dao.jdbc.password>root</dao.jdbc.password> </properties> <activation> <activeByDefault>true</activeByDefault> </activation> </profile> </profiles> </project>
三、dao层配置
1、在dao层resources目录下新建文件mybatis-config.xml
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd"> <configuration> </configuration>
2、在resources目录下新建jdbc.properties文件
c3p0.driverClass=com.mysql.jdbc.Driver c3p0.jdbcUrl=jdbc:mysql://${dao.jdbc.url}:${dao.jdbc.port}/${dao.jdbc.schema}?useUnicode= true&characterEncoding=utf8&mysqlEncoding=utf8&zeroDateTimeBehavior=convertToNull c3p0.user=${dao.jdbc.user} c3p0.password=${dao.jdbc.password} c3p0.acquireIncrement=1 c3p0.maxIdleTime=30 c3p0.minPoolSize=10 c3p0.maxPoolSize=100 c3p0.initialPoolSize=10
3、修改context-dao.xml,新增数据库相关配置
<?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:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:jdbc="http://www.springframework.org/schema/jdbc" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation=" http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd"> <context:annotation-config /> <context:property-placeholder location="classpath:jdbc.properties" ignore-unresolvable="true" /> <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"> <property name="driverClass" value="${c3p0.driverClass}"></property> <property name="jdbcUrl" value="${c3p0.jdbcUrl}"></property> <property name="user" value="${c3p0.user}"></property> <property name="password" value="${c3p0.password}"></property> <property name="acquireIncrement" value="${c3p0.acquireIncrement}"></property> <property name="initialPoolSize" value="${c3p0.initialPoolSize}"></property> <property name="maxIdleTime" value="${c3p0.maxIdleTime}"></property> <property name="maxPoolSize" value="${c3p0.maxPoolSize}"></property> <property name="minPoolSize" value="${c3p0.minPoolSize}"></property> <property name="acquireRetryDelay" value="1000"></property> <property name="acquireRetryAttempts" value="60"></property> <property name="breakAfterAcquireFailure" value="false"></property> </bean> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource" /> </bean> <!-- 使用annotation定义事务 @Transactional --> <tx:annotation-driven transaction-manager="transactionManager" /> <!-- 配置SqlSessionFactoryBean --> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource" /> <property name="configLocation" value="classpath:mybatis-config.xml" /> <property name="mapperLocations" value="classpath:context/mybatis/*Mapper.xml" /> <property name="typeAliasesPackage" value="wang.wangci.bottae.dao.mapper" /> </bean> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value="wang.wangci.bottae.dao.mapper" /> </bean> </beans>
四、编写代码
1、在dao层新建wang.wangci.bottae.dao.entity.User类
package wang.wangci.bottae.dao.entity; public class User { private long id; private String username; private String password; public long getId() { return id; } public void setId(long 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; } }
2、dao层新增接口wang.wangci.bottae.dao.mapper.UserMapper
package wang.wangci.bottae.dao.mapper; import java.util.List; import org.apache.ibatis.annotations.Param; import wang.wangci.bottae.dao.entity.User; public interface UserMapper { public List<User> selects(); public User selectById(@Param("id")long id); }
3、在dao层resources/context文件夹下新建mybatis文件夹,新建接口UserMapper的映射文件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="wang.wangci.bottae.dao.mapper.UserMapper"> <select id="selects" resultType="wang.wangci.bottae.dao.entity.User"> SELECT * FROM user </select> <select id="selectById" resultType="wang.wangci.bottae.dao.entity.User"> SELECT * FROM user WHERE id=#{id} </select> </mapper>
4、在service层新建wang.wangci.bottae.service.UserService接口
package wang.wangci.bottae.service; import java.util.List; import wang.wangci.bottae.dao.entity.User; public interface UserService { public List<User> finds(); public User findById(long id); }
5、在service层新建wang.wangci.bottae.service.impl.UserServiceImpl类
package wang.wangci.bottae.service.impl; import java.util.List; import javax.annotation.Resource; import org.springframework.stereotype.Component; import wang.wangci.bottae.dao.entity.User; import wang.wangci.bottae.dao.mapper.UserMapper; import wang.wangci.bottae.service.UserService; @Component public class UserServiceImpl implements UserService { @Resource private UserMapper userMapper; @Override public List<User> finds() { return userMapper.selects(); } @Override public User findById(long id) { return userMapper.selectById(id); } }
6、在web层新建wang.wangci.bottae.web.controllers.UserController类
package wang.wangci.bottae.web.controllers; import java.util.List; import javax.annotation.Resource; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.ResponseBody; import wang.wangci.bottae.dao.entity.User; import wang.wangci.bottae.service.UserService; @Controller @RequestMapping("/user") public class UserController { @Resource private UserService userService; @ResponseBody @RequestMapping("/gets") public List<User> gets(){ return userService.finds(); } @ResponseBody @RequestMapping("/get") public User get(@RequestParam long id){ return userService.findById(id); } }
五、编译运行
命令行下执行下面命令启动服务:
mvn clean install cd bottae-web mvn jetty:run
访问http://localhost:7777/user/gets,如果将数据库user查询出来了,说明成功了
访问http://localhost:7777/user/get?id=1,如果将id为1的user查询出来了,说明成功了
本文源码:
360云盘下载 访问密码 115f
标签:
原文地址:http://my.oschina.net/funga/blog/484987