码迷,mamicode.com
首页 > 编程语言 > 详细

spring使用mybatis

时间:2019-09-02 11:51:37      阅读:85      评论:0      收藏:0      [点我收藏+]

标签:查找   actor   set   utf-8   ext   -name   nsa   com   配置   

  1. web.xml中增加spring-mybatis.xml文档
1
2
3
4
5
6
7
8
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring-mybatis.xml</param-value>
</context-param>
<!-- Spring监听器 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
  1. 新增spring-mybatis.xml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
<?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:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<!-- 自动扫描 -->
<context:component-scan base-package="com.jishengsoft" />
<!-- 引入配置文档 -->
<bean id="propertyConfigurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location" value="classpath:jdbc.properties" />
</bean>
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<property name="driverClassName" value="${driver}" />
<property name="url" value="${url}" />
<property name="username" value="${username}" />
<property name="password" value="${password}" />
<!-- 初始化连接大小 -->
<property name="initialSize" value="${initialSize}"></property>
<!-- 连接池最大数量 -->
<property name="maxActive" value="${maxActive}"></property>
<!-- 连接池最大空闲 -->
<property name="maxIdle" value="${maxIdle}"></property>
<!-- 连接池最小空闲 -->
<property name="minIdle" value="${minIdle}"></property>
<!-- 获取连接最大等待时间 -->
<property name="maxWait" value="${maxWait}"></property>
</bean>
<!-- 配置sqlSessionFactory -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
</bean>
<!-- DAO接口所在包名,Spring会自动查找其下的类 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.jishengsoft.mapping" />
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>
</bean>
<!-- (事务管理)transaction manager, use JtaTransactionManager for global tx -->
<bean id="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource" />
</bean>
</beans>
  1. 配置数据库的连接文档jdbc.properties(连接的是sqlserver)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
driver=com.microsoft.sqlserver.jdbc.SQLServerDriver
url=jdbc:sqlserver://127.0.0.1:1433;databaseName=swgldemo
username=sa
password=123456
#定义初始连接数
initialSize=0
#定义最大连接数
maxActive=20
#定义最大空闲
maxIdle=20
#定义最小空闲
minIdle=1
#定义最长等待时间
maxWait=60000
  1. 新增实体类User.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
package com.jishengsoft.pojo;
public class User {
private String userName;
private String password;
private String shopCode;
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;
}
public void setShopCode(String shopCode) {
this.shopCode = shopCode;
}
public String getShopCode() {
return shopCode;
}
}
  1. 新增Dao接口UserMapper.java
1
2
3
4
5
6
7
8
9
10
11
12
package com.jishengsoft.mapping;
import java.util.List;
import org.apache.ibatis.annotations.Select;
import com.jishengsoft.pojo.User;
public interface UserMapper {
@Select("select 操作员 as userName,口令 as password from 操作员")
List<User> getUserList();
}
  1. 在service或controller调用Dao接口
1
2
3
4
@Resource //自动实例化
private UserMapper userMapper;
userMapper.getUserList();
  1. 结果展示
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
[
{
userName: "ghl",
password: "30303030303030303030303030303030",
shopCode: null
},
{
userName: "jsuser",
password: "303630303030303030303030303030303639433344313342354142443038394635313336434233303935383034434532",
shopCode: null
},
{
userName: "采购员",
password: "30303030303030303030303030303030",
shopCode: null
},
{
userName: "仓管员",
password: "30303030303030303030303030303030",
shopCode: null
},
{
userName: "系统管理员",
password: "30303030303030303030303030303030",
shopCode: null
},
{
userName: "销售前台",
password: "30303030303030303030303030303030",
shopCode: null
},
{
userName: "总经理",
password: "30303030303030303030303030303030",
shopCode: null
}
]

原文:大专栏  spring使用mybatis


spring使用mybatis

标签:查找   actor   set   utf-8   ext   -name   nsa   com   配置   

原文地址:https://www.cnblogs.com/petewell/p/11445362.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!